web123456

JavaScript - Operators

catalogs

operator (computing)

arithmetic operator

1. Concepts

2. Expressions and return values

Incrementing and decrementing operators

4. Incremental operators

5. Increasing and decreasing summaries

comparison operator

summarize

logical operator

1. Overview

2. Logic and &&.

3. Logical or | |

4. Logical non !

5. Logic interrupt logic and

6. Logic interrupt logic or

assignment operator

operator priority


operator (computing)

Operators are also known asoperator (computing), are symbols used to implement functions such as assignments, comparisons, and performing arithmetic operations.

arithmetic operator

1. Concepts

Concept: symbols used in arithmetic operations to perform arithmetic operations on two variables or values

operator (computing) descriptive an actual example
+ plus 10+20=30
- diminish 10-20=-10
* multiply (mathematics) 10*20=200
/ get rid of 10/20=0.5
% Taking a remainder (modulo) Return the remainder of the division 9%2=1
  1. (1 + 1); // 2
  2. (1 - 1); // 0
  3. (1 * 1); // 1
  4. (1 / 1); // 1
  5. // 1. % Reduction (modulo)
  6. (4 % 2); // 0
  7. (5 % 3); // 2
  8. (3 % 5); // 3
  9. // 2. Floating point arithmetic can be problematic.
  10. (0.1 + 0.2); // 0.30000000000000004
  11. (0.07 * 100); // 7.000000000000001
  12. // 3. We can't just take floating point numbers and compare them for equality.
  13. var num = 0.1 + 0.2;
  14. (num == 0.3); // false

2. Expressions and return values

Expression: a combination of numbers, operators, variables, etc., arranged in a meaningful way that yields a value.

  1. // is an equation made up of numbers, operators, variables, etc. We become the expression 1 + 1
  2. console.log(1 + 1); // 2 is the return value
  3. // 1 + 1 = 2
  4. // In our program, 2 = 1 + 1 takes our right-hand expression and gives the return value to the left-hand side.
  5. var num = 1 + 1;

Incrementing and decrementing operators

1. Front-loaded incremental

If you need to repeatedly add or subtract 1 to or from the number halo, you can use the increment (++) and decrement (-- ) operators to do so.
existJavaScriptIn this case, increment (++) and decrement (--) can be placed either before or after the variable.precede a variableWhen we can callPreceding incremental (decremental) operators, placed after the variable presentationWhen we can callPosterior Incremental (Decremental) Operators
Note: The increment and decrement operators must be used with variables.
 

First add 1, then return value

  1. // 1. Wanting a variable to add 1 to itself num = num + 1 is a bit tricky
  2. var num = 1;
  3. num = num + 1; // ++num
  4. num = num + 1;
  5. console.log(num); // 3
  6. // 2. The pre-increment operator ++ is written in front of the variable.
  7. var age = 10;
  8. ++age; // Similar to age = age + 1
  9. console.log(age);
  10. // 3. First add 1 and then return the value
  11. var p = 10;
  12. console.log(++p + 10);

2. Posterior increment

Mnemonic: first return to the original value, then add 1

  1. var num = 10;
  2. num++; // num = num + 1 ++num;
  3. console.log(num);
  4. // 1. Pre- and post-increment have the same effect if used separately.
  5. // 2. Post-Increment Mnemonic: first return the original value, then add 1.
  6. var age = 10;
  7. console.log(age++ + 10);//20
  8. console.log(age);//11

4. Incremental operators

  1. var a = 10;
  2. ++a; // ++a 11 a = 11
  3. var b = ++a + 2; // a = 12 ++a = 12
  4. console.log(b); // 14
  5. var c = 10;
  6. c++; // c++ 11 c = 11
  7. var d = c++ + 2; // c++ = 11 c = 12
  8. console.log(d); // 13
  9. var e = 10;
  10. var f = e++ + ++e; // 1. e++ = 10 e = 11 2. e = 12 ++e = 12
  11. console.log(f); // 22
  12. // Posterior self-incrementation: first the expression returns the original value, then the variable is incremented by 1.

5. Increasing and decreasing summaries

  • The pre-increment and post-increment operators simplify the writing of code, making the value +1 of a variable simpler to write than before
  • When used alone, the result is the same
  • When used in conjunction with other code, the result will be different.
  • Posterior: original value operation first, then self-addition (first man, then self)
  • Prefix: self-addition followed by arithmetic (self followed by others)
  • When developing, mostly use post-increment/decrement and have the code on a single line, e.g. num++; or num--;

comparison operator

summarize

Concept: Comparison operator (relational operator) is an operator used when comparing two data, after the comparison operation, it will return a boolean value ( true / false ) as the result of the comparison operation.

Operator Name clarification case (law) in the end
< less than sign = (math.) 1<2 true
> Chinese blank character □ (punct. used for emphasis) 1>2 false
>= Greater than or equal to sign (greater than or equal to) 2>=2 true
<= Less-than-equal sign (less than or equal to) 3<=2 false
== Pre-equalization (will be transformed) 37==37 true
!= inequality sign (i.e. not equal, ≠ or greater than >, or less than <) 37!=37 false
===         !== Equality requires that the values and data types are the same. 37===‘37’ false
  1. console.log(3 >= 5); // false
  2. console.log(2 <= 4); // true
  3. //The equals sign in our program is ==. The default conversion type converts data from a string to a number.
  4. // only requires the values to be equal
  5. console.log(3 == 5); // false
  6. console.log('Math teacher' == 'Andy Lau'); // flase
  7. console.log(18 == 18); // true
  8. console.log(18 == '18'); // true
  9. console.log(18 != 18); // false
  10. // 2. We have equality in our program, which requires that the values and data types on both sides be identical. true
  11. console.log(18 === 18);
  12. console.log(18 === '18'); // false

logical operator 

1. Overview

Concept: A logical operator is an operator used to perform boolean operations, and its return value is also a boolean value. It is often used to determine multiple conditions in later development

logical operator clarification case (law)
&& "Logic and", short for "and" and true && false
| | "Logical or", short for "or" or true | | false
! ""Logical non"", for short ""non"" not ! true

2. Logic and &&.

If both sides are true, the result is true. If one side is false, the result is false.

  1. // Logical && and both sides are true, result is true, as long as one side is false, result is false.
  2. console.log(3 > 5 && 3 > 2); // false
  3. console.log(3 < 5 && 3> 2); // true

3、logical or | | 

False on both sides results in false false, as long as one side is true, the result is true.

  1. // Logical or || or Both sides are false, the result is false false As long as one side is true, the result is true.
  2. console.log(3 > 5 || 3 > 2); // true
  3. console.log(3 > 5 || 3 < 2); // false

4. Logical non !

Logical non(!) t is also called an inverse, and is used to take the opposite of a boolean value, e.g. the opposite of true is false

  1. // Logically not not!
  2. console.log(!true); // false

5. Logic interrupt logic and

1. Short-circuit operation (logic interrupt)
Principle of short-circuit arithmetic∶:: When there is more than one expression (value), and the value of the expression on the left can determine the result, then it will not continue to operate on the value of the expression on the right.

2. Logic and

  • Syntax: Expression 1 && Expression 2
  • If an expression evaluates to true, return expression 2
  • If an expression evaluates to false, return expression 1
  1. // 1. Logical operations with our boolean values true & & false == false
  2. // 2. Is 123 && 456 a value or an expression involved in a logical operation?
  3. // 3. Logical and short-circuiting operations Return expression 2 if expression 1 is true Return expression 1 if expression 1 is false
  4. console.log(123 && 456); // 456
  5. console.log(0 && 456); // 0
  6. console.log(0 && 1 + 2 && 456 * 56789); // 0
  7. console.log('' && 1 + 2 && 456 * 56789); // ''
  8. // False if empty or negated, true otherwise 0 '' null undefined NaN

6. Logic interrupt logic or

1. Logic interrupt (short-circuit operation)

  • Syntax: expression1 || expression2
  • If an expression evaluates to true, return expression 1
  • If an expression evaluates to false, return expression 2
  1. // logical or short-circuit operation if expression 1 is true then expression 1 is returned if expression 1 is false then expression 2 is returned
  2. console.log(123 || 456); // 123
  3. console.log(123 || 456 || 456 + 123); // 123
  4. console.log(0 || 456 || 456 + 123); // 456
  5. // Logic interrupts are important, they affect the outcome of our programs, Smithsonian.
  6. var num = 0;
  7. console.log(123 || num++);// front operation, back operation is not involved
  8. console.log(num); // 0

assignment operator

Concept: Operators used to assign data to variables

assignment operator clarification case (law)
= direct assignment var usrName='I am value';
+=、-= Add or subtract a number before assigning it var age=10;age++;//15
*=、/=、%= Multiply, divide, and take a modulus before assigning a value. var age=2;age*=5;//10
  1. var num = 10;
  2. // num = num + 1; num++
  3. // num = num + 2; // num += 2;
  4. // num += 2;
  5. num += 5;
  6. console.log(num);//15
  7. var age = 2;
  8. age *= 3;
  9. console.log(age);//6

operator priority

  • The logical non-priority inside a unary operator is the highest priority
  • Logical and has higher priority than logical or
  1. console.log(4 >= 6 || 'People' != 'Avatar' && !(12 * 2 == 144) && true);//true
  2. var num = 10;
  3. console.log(5 == num / 2 && (2 + 2 * num).toString() === '22');//true
  4. var a = 3 > 5 && 2 < 7 && 3 == 4;
  5. console.log(a);//false
  6. var b = 3 <= 4 || 3 > 1 || 3 != 2;
  7. console.log(b);//true
  8. var c = 2 === "2";
  9. console.log(c);//false
  10. var d = !c || b && a;
  11. console.log(d);//true