JavaScript Basics - Operators (3)
Article directory
- JavaScript Basics - Operators (3)
1. What is an operator
Operators, also known as operators, are symbols used to implement functions such as assignment, comparison, and arithmetic operations.
2. Expressions and return values
An expression is a combination of numbers, operators, variables, etc. that can obtain values and meaningful arrangements. Generally speaking, it is a formula composed of numbers, variables, and operators. For example: 1+1=2, 100-100 = 0. From the above example, we know that the expression will have a result, which is returned to us, we call it the return value
copy// In the program, we often assign the return value to a variable var sum = 12 + 34; // Calculate the return value on the right and assign it to the variable on the left console.log(sum); // The result is 46
3. Overview of Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on two variables. details as follows:
- Addition, subtraction, multiplication, and division are similar to mathematical operations. The operator = is used to assign values to js variables:
copyvar x = 11; // assign 12 to the x variable var y = 22; var num1 = x + y; // Adds the values of two variables var num2 = x - y; // Subtract two numbers var num3 = x * y; // multiply two numbers var num4 = x / y; // Divide two numbers console.log(num1); // The result is 33 console.log(num2); // -11 console.log(num3); // 242 console.log(num4); // 0.5
Note: When calculating floating-point numbers, the output will be problematic, because the highest precision of floating-point numbers is 17 decimal places:
copyvar num1 = 0.1; var num2 = 0.2; var sum = num1 + num2; console.log(sum); // The output is 0.30000000000000004 // So two floating point numbers cannot be compared for equality
- The remainder symbol is the same as the division sign in mathematics, but the two are just similar in appearance, but their functions are quite different. The remainder is to divide the two, take the remainder, and the remainder is always consistent with the sign of the dividend. The specific code is as follows:
copyvar num1 = 15; var num2 = 3; var num3 = -25; var num4 = 7; var x = num1 % num2; var y = num3 % num4; console.log(x); // 0 console.log(y); // -4
Note: If the dividend is NaN, the result is NaN.
copyvar result = NaN % 2; console.log(result); // The result is NaN
4. Assignment operator
Assignment operators are used to assign values to JavaScript variables, as follows:
copy// = is direct assignment var num = 15; // +=, -=, *=, /=, %= is the first operation and then assignment num += 1; // Equivalent to num = num + 1 num -= 1; // Equivalent to num = num - 1 num *= 10; // Equivalent to num = num * 10
5. Increment and Decrement Operators
5.1 Overview of increment and decrement operators
To repeatedly add or subtract one to a numeric variable, you can use the increment operator ( ++ ) and the decrement operator ( – ). In js, the increment and decrement operators can be placed in front of the variable or in the After the variable, note that it must be used in conjunction with the variable:
- When placed in front of a variable, we call it a pre-increment (decrement) operator
- After the variable is what we call the post-increment (decrement) operator
5.2 Increment operator
5.2.1 Pre-increment operator
The pre-increment operator is written in front of the variable, such as: ++num, which means self-increment, which is equivalent to num = num + 1, but ++num is simpler.
copyvar age = 18; ++age; // Equivalent to age = age + 1
5.2.2 Post-increment operator
The post-increment operator is written after the variable, which also means self-increment, and the writing method is simpler. It returns the original value first and then self-increment.
copyvar age = 20; age++; // Equivalent to age = age + 1
5.2.3 Difference between Postfix and Prefix Operators
If the two are used alone, the effect is the same, but there is a little difference in the calculation process. When used with other codes, the execution results will be different. The preposition is to return the value after the self-increment, and the postposition is to return the value first, and then Add from:
copy// pre-increment operator var num = 10; console.log(++num + 1); // The result is 12, because num first increases from 1 to 11, returns, and adds 1 // post-increment operator var age = 15; console.log(age++ + 5); // The result is 20, because the postfix returns the original value first, and then adds 1 console.log(age); // The result is 16, which is the result of self-addition
Note: Post-increment (decrement) operators are generally used in development.
6. Comparison Operators
Comparison operators are used for comparison between two data, and the operation result is a Boolean value
copyconsole.log(2 == 4); // false console.log(54 <= 134); // true console.log(19 === '19'); // false, the congruent symbol requires that the values and data types on both sides are exactly the same to be true (true)
7. Logical operators
7.1 Overview of Logical Operators
Logical operators are mainly used to perform Boolean operations, and their return values are Boolean values, which are mainly used for conditional judgment:
7.2 Logical AND
&& (logical AND) returns true only if the values on both sides are true, otherwise returns false
copyvar result = 2 > 1 && 5 > 3; // true var outcome = 10 > 100 && 4 > 1; // false
7.3 Logical OR
|| (logical OR) returns true as long as one of them is true, and returns false if both sides are false
copyvar result = 34 > 12 || 12 > 34; // true var outcome = 23 < 12 || 56 < 45; // false
7.4 Logical NOT
Logical not (!) is also called negation, which is used to take the opposite value of a Boolean value, such as the opposite value of true is false
copyvar sayOk = !true; console.log(sayOk); // false
7.5 Logic and short-circuit operation (logic interrupt)
Short-circuit operation: When there are multiple expressions (values), the value of the expression on the left can be determined, and the expression on the right will not be evaluated.
Logical and short-circuit operations:
- Syntax: expression1 && expression2
- If expression 1 is true, return expression 2 (true return other)
- If expression1 is false, return expression1 (return from false from)
copyconsole.log(0 && 23); // The result is 0, since 0 is false, return 0 itself console.log(123 && 356); // The result is 356, since 123 is true, returns 356
7.6 Logical or short-circuit operation
- Syntax: expression1 || expression2
- If expression1 is true, return expression1 (return true from)
- If expression 1 is false, return expression 2 (return other from false)
copyconsole.log(123 && 345); // The result is 123 console.log(0 && 467); // 467
7.7 Application of short-circuit calculation
Logical interruption is very important, it will directly affect the running results of the program:
copyvar age = 18; console.log(345 || age++); // 345 is true, return, do not execute age++ console.log(age); // The value is still 18, because age++ is not executed
8. Operator precedence
- Logical AND has a higher priority than logical OR
- Logical non-precedence in unary operators is high
- Higher priority first, then lower priority
- Equal priority is counted from left to right