JavaScript operator (computing)
Operators (operator ), also known as operators, are symbols used to implement functions such as assigning, comparing, and performing arithmetic operations.
-
Arithmetic operators:
Used to perform arithmetic operations on two variables or values + - * / %
console.log(0.1 + 0.2); // 0.30000000000004
- 1
Note:
Floating-point numbers have problems with arithmetic because they're already very small, so you have to convert them to binary and then do the arithmetic later on.
So you can't compare floating-point numbers directly to see if they are equal.
- 1
- 2
- 3
-
Preceding increment operator (++i)
First add 1, then return the value -
Posterior increment operator (i++)
Returns the original value first, then adds 1 to it.
Front self-incrementing and back self-incrementing have the same effect if used individually
Most of the development uses post-incrementation
var e = 10;
var f = e++ + ++e; //1. e++ After returning 10, add 1 to 11.
console.log(f); // 22
- 1
- 2
- 3
-
comparison operator
The comparison operator (relational operator) is an operator used when comparing two pieces of data and returns a boolean value ( true / false ) as the result of the comparison operation.
console.log(18 == '18'); // true
console.log(18 === '18'); // false
- 1
- 2
Note:
1. Inside the program, == will convert the data type by default.
2. === and ! == are equivalent, requiring the same value and data type.
- 1
- 2
- 3
-
logical operator
Logical operators are operators used to perform boolean operations, and their return values are also boolean values. They are often used in later development to determine multiple conditions
Logical with: & & ; Logical or: || ; Not ! ||; non!
Short-circuit arithmetic (logic interrupt)
Principle of short-circuit arithmetic: When there are several expressions (values), 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.
1. Logic and
Syntax: Expression 1&&Expression 2
If the value of the first expression is true, expression 2 is returned
If the value of the first expression is false, return expression 1
2. Logical or
Syntax:Expression1||Expression2
If the value of the first expression is true, expression 1 is returned
If the value of the first expression is false, expression 2 is returned
<script>
// 1. Logical operation true && false == false with our boolean values.
// 2.123 && 456 is the value or expression involved in the logical operation?
console.log(123 &&456); // 456
console.log(0 && 456);//o
console.log(0 && 1 +2 && 456 *56789); // 0
console.log('' && 1 + 2 && 456 * 56789); //''
//false if empty or 0 and negated the rest are true 0'' null undefined NaN false
console.log(123 || 456);1l/ 123
console.log(123 || 456 || 456 + 123);//123
console.log( || 456 || 456 +123);// 456
//Logic interrupts are important as they affect the outcome of our program.
var num = 0;
console.log(123 || num++);
console.log(num); // 0
</ script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
What is the use of short-circuit operation of && and ||?
1, due to && and || expression there is a short-circuit operation phenomenon, after the short-circuit will not operate on the subsequent expression, so the expression containing && and || is best avoided.
Therefore, expressions containing && and || should not be used for variable assignment and operation, but should be calculated first for each expression.
You can calculate the result of each expression first, and then directly take the result to do the operation of && and ||.
This should be noted when writing code to prevent short-circuiting of && and || from leading to the writing of the code.
The result is different from what is expected.
2, to quickly determine the results of the entire expression true or false expression in front, so that due to the short-circuit operation behind the expression may not be calculated
so that due to the short-circuit operation after the expression may not be calculated to save the processor computing time.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
-
assignment operator
operator priority
Logical and takes precedence over logical or