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); // 2
-
(1 - 1); // 0
-
(1 * 1); // 1
-
(1 / 1); // 1
-
// 1. % Reduction (modulo)
-
(4 % 2); // 0
-
(5 % 3); // 2
-
(3 % 5); // 3
-
// 2. Floating point arithmetic can be problematic.
-
(0.1 + 0.2); // 0.30000000000000004
-
(0.07 * 100); // 7.000000000000001
-
// 3. We can't just take floating point numbers and compare them for equality.
-
var num = 0.1 + 0.2;
-
(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.
-
// is an equation made up of numbers, operators, variables, etc. We become the expression 1 + 1
-
console.log(1 + 1); // 2 is the return value
-
// 1 + 1 = 2
-
// In our program, 2 = 1 + 1 takes our right-hand expression and gives the return value to the left-hand side.
-
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. Wanting a variable to add 1 to itself num = num + 1 is a bit tricky
-
var num = 1;
-
num = num + 1; // ++num
-
num = num + 1;
-
console.log(num); // 3
-
// 2. The pre-increment operator ++ is written in front of the variable.
-
var age = 10;
-
++age; // Similar to age = age + 1
-
console.log(age);
-
// 3. First add 1 and then return the value
-
var p = 10;
-
console.log(++p + 10);
2. Posterior increment
Mnemonic: first return to the original value, then add 1
-
var num = 10;
-
num++; // num = num + 1 ++num;
-
console.log(num);
-
// 1. Pre- and post-increment have the same effect if used separately.
-
// 2. Post-Increment Mnemonic: first return the original value, then add 1.
-
var age = 10;
-
console.log(age++ + 10);//20
-
console.log(age);//11
4. Incremental operators
-
var a = 10;
-
++a; // ++a 11 a = 11
-
var b = ++a + 2; // a = 12 ++a = 12
-
console.log(b); // 14
-
-
var c = 10;
-
c++; // c++ 11 c = 11
-
var d = c++ + 2; // c++ = 11 c = 12
-
console.log(d); // 13
-
-
var e = 10;
-
var f = e++ + ++e; // 1. e++ = 10 e = 11 2. e = 12 ++e = 12
-
console.log(f); // 22
-
// 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 |
-
console.log(3 >= 5); // false
-
console.log(2 <= 4); // true
-
//The equals sign in our program is ==. The default conversion type converts data from a string to a number.
-
// only requires the values to be equal
-
console.log(3 == 5); // false
-
console.log('Math teacher' == 'Andy Lau'); // flase
-
console.log(18 == 18); // true
-
console.log(18 == '18'); // true
-
console.log(18 != 18); // false
-
// 2. We have equality in our program, which requires that the values and data types on both sides be identical. true
-
console.log(18 === 18);
-
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.
-
// Logical && and both sides are true, result is true, as long as one side is false, result is false.
-
console.log(3 > 5 && 3 > 2); // false
-
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.
-
// Logical or || or Both sides are false, the result is false false As long as one side is true, the result is true.
-
console.log(3 > 5 || 3 > 2); // true
-
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
-
// Logically not not!
-
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. Logical operations with our boolean values true & & false == false
-
// 2. Is 123 && 456 a value or an expression involved in a logical operation?
-
// 3. Logical and short-circuiting operations Return expression 2 if expression 1 is true Return expression 1 if expression 1 is false
-
console.log(123 && 456); // 456
-
console.log(0 && 456); // 0
-
console.log(0 && 1 + 2 && 456 * 56789); // 0
-
console.log('' && 1 + 2 && 456 * 56789); // ''
-
// 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
-
// 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
-
console.log(123 || 456); // 123
-
console.log(123 || 456 || 456 + 123); // 123
-
console.log(0 || 456 || 456 + 123); // 456
-
// Logic interrupts are important, they affect the outcome of our programs, Smithsonian.
-
var num = 0;
-
console.log(123 || num++);// front operation, back operation is not involved
-
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 |
-
var num = 10;
-
// num = num + 1; num++
-
// num = num + 2; // num += 2;
-
// num += 2;
-
num += 5;
-
console.log(num);//15
-
var age = 2;
-
age *= 3;
-
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
-
console.log(4 >= 6 || 'People' != 'Avatar' && !(12 * 2 == 144) && true);//true
-
var num = 10;
-
console.log(5 == num / 2 && (2 + 2 * num).toString() === '22');//true
-
-
-
var a = 3 > 5 && 2 < 7 && 3 == 4;
-
console.log(a);//false
-
-
var b = 3 <= 4 || 3 > 1 || 3 != 2;
-
console.log(b);//true
-
-
var c = 2 === "2";
-
console.log(c);//false
-
-
var d = !c || b && a;
-
console.log(d);//true