Easy to learn about JavaScript 6: JavaScript expressions and operators
The JavaScript scripting language describes a set of operators used to operate data values, including unary operators, boolean operators, Arithmetic Operators, and Relational operators.
Operator, ternary operator, bitwise operator, and value assignment operator.
An expression is a "phrase" in JavaScript language that contains the variable name (or literal) and operator. The simplest expression is the literal or variable name. When
A simple expression can also be merged to create a complex expression.
One-to-one operators
(1) Increment ++ and decrease --
Var box1 = 100; ++ box1; // equivalent to box = box + 1 document. write ("box1 =" + box1 + ""); // output box1 = 101 var box2 = 100; -- box2; // equivalent to box = box2-1 document. write ("box2 =" + box2); // output box2 = 99
Difference between front and back
Var box = 100; var age = ++ box; // box first accumulates 1 to 101, and then assigns the age to 101 var height = box ++; // box first assigns height to 101, and box accumulates to 102 document. write ("age =" + age + ""); // output age = 101 document. write ("height =" + height + ""); // output height = 101 document. write ("box =" + box); // output box = 102, because the box has been accumulated twice, so it is 102
In the absence of a value assignment operation, the front and back are the same. However, in the Value assignment operation, if the increment or decrease operator is prefixed, the prefix operator first
Accumulate, subtract, and then assign values. If it is a post-operator, assign values first and then accumulate or subtract.
(2) addition and subtraction Operators
It is used for positive or negative operations, and can also be used to convert numeric strings into numeric forms.
Var box = "20"; document. write (typeof box + ""); // output string var age =-box; document. write (age + ""); // output-20 document. write (typeof age); // output number
Binary Arithmetic Operators
The JavaScript language specifies five Arithmetic Operators, namely +,-, *,/, and % (remainder ). If the value of the arithmetic operator is not a value, it will
First, use the Number () transformation function to convert it to a numerical value (implicit conversion ).
Var box = 100 + "100"; document. write ("box =" + box + ""); // output 100100 document. write (typeof box); // output string
Why? When performing arithmetic operations in JavaScript, if one of them is a string, the result is converted to a string. Equivalent
It is a string connector and cannot be counted as an addition arithmetic operator.
Var box = "100"-10; document. write ("box =" + box + ""); // output 90 var age = 5/4; document. write ("age =" + age + ""); // output 1.25 var height = ("your age is:" + (10 + 10 )); // brackets force priority document. write (height); // output your age: 20
Remainder
Var box = 10% 3; document. write ("box =" + box); // output 1
Three-link Operator
The operators used for comparison are called Relational operators: <(less than),> (greater than), <= (less than or equal to), >=( greater than or equal ), = (relative ),! = (No
Etc.), === (constant or full ),! = (Incomplete or not constant ). Most Relational operators return a Boolean value.
Like other operators, Relational operators must follow the following rules when operating non-numeric values:
1. If both operators are numerical values, the values are compared.
2. If both operands are strings, the character encoding values corresponding to the two strings are compared.
3. If one of the two operands is a numerical value, the other is converted to a numerical value.
4. If either of the two operands is an object, call the value () method or toString () method first and then compare the result.
Var box1 = 3> 2; document. write (box1 + ""); // Output true var box2 = "3"> 22; document. write (box2 + ""); // output false var box3 = "3"> "22"; document. write (box3 + ""); // Output true var box4 = "a"> "B"; // a is 97, B is 66 document. write (box4 + ""); // Output true var box5 = "Blue" <"alpha"; // The first letter of Blue is B, the first letter of alpha is a, a is 97, and B is 66 document. write (box5) // outputs true
In comparison of equal and unequal values, if the operands are non-numeric values, the following rules are followed:
1. If an operand is a Boolean value, convert it to a value before comparison, convert false to 0, and convert true to 1.
2. If an operand is a string, convert it to a value before comparison.
3. If an operand is an object, call the value () method or toString () method before comparison.
4. If no conversion is required, null and undefined are equal.
If an operand of 5 is NaN, = returns false ,! = True is returned, and NaN is different from itself.
6. If both operands are objects, check whether they are the same object. If they all point to the same object, true is returned; otherwise, false is returned.
7. true is returned only when the values and types are equal in the case of full equality and full inequality. Otherwise, fasle is returned.
Var box1 = '2' = 2; document. write (box1 + ""); // The output is true. Only the value var box2 ={}={} is compared. document. write (box2 + ""); // The output is false. Because their addresses are compared, the reference addresses of each newly created object are different. Var box3 = null = undefined; document. write (box3 + ""); // Output true, because both are null values var box4 = '2' = 2; document. write (box4 + ""); // The output is false. The data types of the two operands are not equal. var box5 = null === undefined; document. write (box5); // output false. The data types of the two operands are not equal.
Four logical operators
Logical operators in JavaScript usually act on Boolean operations. They are generally used together with Relational operators. There are three logical operators :&&
(Logical and), | (logical or) and! (Not logically ).
(2) & indicates that both sides must be true to return true.
If one of the operands on both sides is not a Boolean value, a Boolean value is not necessarily returned for the operation. The following rule is followed:
1. If the first operand is an object, the second operand is returned.
2. If the second operand is an object, true is returned for the first operand. Otherwise, false is returned.
3. If an operand is null, null is returned.
4. If an operand is undefined, undefined is returned.
5. If one operation is an object and the other is a Boolean value, this object is returned.
The logic and operator are short-circuit operations. If the first operand returns false and the second operand returns false, both true and false.
Var box1 ={}& & (5> 4); document. write (box1 + ""); // Output true var box2 = (5> 4) & {}; document. write (box2 + ""); // output [object Object] var box3 = (3> 4) & {}; document. write (box3); // output false
(2) | returns true if either of them is true.
If one of the operands on both sides is not a Boolean value, a Boolean value is not necessarily returned for the operation. The following rule is followed:
1. If the first operand is an object, the first operand is returned.
2 If the evaluate result of the first operand is fasle, the second operand is returned.
3. If both operands are objects, the first operand is returned.
4 if both operands are null, null is returned.
If both operands are undefined, the return value is undefined.
If both operands are NaN, NaN is returned.
The logic or operator is also a short-circuit operation. If the first operand returns true, the second returns true whether it is true or false.
Var box1 ={}| | (5> 4); document. write (box1 + ""); // output [object Object] var box2 = (5> 4) ||{}; document. write (box2 + ""); // Output true var box3 = (3> 4) |{}; document. write (box3); // output [object Object]
(3 )! Logical non-operators can be used with any value. No matter what data type the value is, this operator returns a Boolean value and its flow
Yes: first convert the value to a Boolean value and then take the inverse value. The rules are as follows:
1. the operand is an object and false is returned.
2. returns true if the operand is an empty string.
3. If the operand is a non-null string, false is returned.
4. If the operand is 0, true is returned.
5. If the operand is any non-0 value, false is returned.
6. If the operand is null, true is returned.
7. If the operand is NaN, true is returned.
8. If the operand is undefined, true is returned.
Var box =! {}; Document. write (box); // output false
Five-digit Operator
JavaScript includes seven bitwise operators :~ (Non-bitwise), & (bitwise AND), | (bitwise OR), ^ (bitwise OR), <(left shift), >>( there is a shift to the right ),
>>> (Shifts right unsigned)
(1) Non (~) The operation converts the operation number to a 32-bit number, converts the binary number to its binary anticode, and finally converts the binary number to a floating point number.
Number. In essence, it is to calculate the negative value for the number, and then minus 1 is the obtained value.
Var box = ~ 25; document. write (box); // output-26
(2) bitwise AND (&) operations directly perform operations on the binary form of the number, and then perform operations on the two digits in the same position up and down, with only two digits
1 is obtained only when the value is 1, and the rest are 0.
Var box = 25 & 3; document. write (box); // output 1
(3) The bitwise OR (|) operation is to directly calculate the binary form of the number, and then perform the or operation on the two digits in the upper and lower positions, with only two digits on the right
0 is obtained only when the bits are 0, and the rest are 1.
Var box = 25 | 3; document. write (box); // output 27
(4) bitwise XOR (^) is also a direct binary operation. If only one digit stores 1, it returns 1. The rest return 0. That is
Two digits return 0 at the same time, and 1 at the same time.
Var box = 25 ^ 3; document. write (box); // output 26
(5) The Left shift operation is also an operation on the binary number, which is equal to the product where the first operand is multiplied by (the power of the number of left shifted digits of 2.
Var box = 25 <3; document. write (box); // 25 shifts three digits to the left, which is equivalent to 25 multiplied by (3 power of 2), so the output is 200
(6) The signed right shift operation is also an operator that operates on binary numbers, equal to dividing the first operand by (the right shift digit power of 2.
Var box = 24> 2; document. write (box); // output 6
(7) The unsigned right shift operation also operates on binary numbers. For positive numbers, it is the same as the signed right shift operation, but for negative numbers, there will be some
Different.
For more information about ECMAScript bit operators, visit ECMAScript bit operators.
Six assignment operators
Assignment operators include: = (), + = (),-= (), * = (),/= (), % = (), <= (), >>= (), >>>= ().
Var box = 100; box + = 100; // equivalent to box = box + 100 document. write ("box =" + box); // output box = 200
Seven other operators
1 string OPERATOR: "+", which is used to add two strings. Rule: Only one string is needed.
Var box = 100 + "10" '; document. write ("box =" + box); // output 100100
2. Comma operator. Multiple operations can be performed in one statement.
Var box = 100, age = 200, height = 300; document. write ("box =" + box); // output box = 100
3. Ternary operators:
Var box = (3> 4 )? "Right": "error"; document. write (box); // Output Error
For more details about ECMAScript operators, visit ECMASscript unary operators in the JavaScript advanced tutorial. This
Detailed operator tutorials are provided in the series. For JavaScript operators, we can compare C ++, C #, and Java. This is quite easy.
.