- An expression
- Unary operator
- Priority level
- Binding nature
- Order of Operations
What is an expression?
is a phrase in JS, the interpreter encountered this phrase will be to calculate it, get a result to participate in the operation, we have to participate in the operation of the various phrases called expressions. In fact, JS code is composed of expressions and operators, you can say that in addition to the operator is basically an expression.
For example: A + 1; In this line of code, the plus sign to the left of a and 1 on the right is the expression, and the plus sign is the operator.
Classification of expressions (probably divided into 6 categories)
1, original expression (4 kinds): constant, variable, direct amount, keyword
2, initialize the expression:
- var obj = {name: ' Suki};
- var arr = [1, 2, 3];
3, Function-definition expression: Functions foo () {}
4, Function call expression: foo ();
5, property access expression: Obj.name; Obj[name];
6, Object creation expression: New Foo ();
Unary operator
You can only manipulate the symbols of one expression, a total of 9;
One dollar Plus
- The number of a unary plus operation, the result returns the number itself, to pay attention to negative numbers for a unary plus operation results or negative, do not think you will get an integer;
- A unary plus operation on a Boolean type, TRUE returns 1,false returns 0;
- A unary plus operation on NULL, returning 0;
- A unary plus operation is performed on the undefined, returning nan;
- A unary plus operation on a string, which has two results, returns 1 if the string is composed of a pure number, or Nan if not;
- A unary plus operation on the object, there are two results, one is to return the number, and the second is to return Nan, the operation of the first call the ValueOf method, if the number results are not obtained, call the ToString method, and then the resulting string to a unary plus operation;
Purpose: Converts an expression into a number;
One Yuan minus
- Use for numbers: Returns the negative numbers of the digits;
- For other data types use: First try to convert the expression to a number, and then take negative numbers;
var false ; -B; // -0
First convert false into a number to get 0, and then take 0 negative numbers to get-0, in JS +0 = = = 0;//true;
Predecessor increment, decrement operator
The interpreter first adds one or more actions to the expression, and then lets the expression participate in the subsequent operation;
var num1 = 2; var num2 =; --num 1 + num2; // 1 +
Post increment, decrement operator
Let the expression participate in the operation, and then add one or minus one of the expression, the usual post-increment, decrement operator used a little more;
var num1 = 2; var num2 =+ num2; // 2 +num1; // 1
var age = +; ++age; // A // if the operand is a number, the two are the same: age = age + 1; // A
But when it comes to string expressions, things are different.
var name = ' Suki '; ++name; // NaN // as long as one operand is a string, the plus sign becomes the string connector name = name + 1; // ' Suki1 '
Priority level
As in the mathematical expression, the first calculation multiplication plus minus, because the priority of multiplication is higher than the addition and subtraction;
Property access > Unary operators > Multiplication > Add and subtract > Compare > Equals > and Operations > or Operations > Three mesh Operations > Assignment operations
Do not remember so much, just remember the following three points is good:
- The property access expression has the highest priority;
- The assignment operation (=) has the lowest priority (in fact, the comma is lower than the equal sign, but the usual comma does not participate in the operation);
- In all operators, the unary operator has the highest priority;
var a = 3; // false;
Because the precedence of equal (= =) is lower than the increment operator (unary operator), the ++a is computed first, the result is 4, then the 4==3 is computed, and the return is false;
Binding nature
How is it calculated when there are multiple operators in a line of code, and the precedence of these operators is equal? This leads to the problem of integration;
- Left combination
- Right combination
In fact, from the left to calculate or from the right to start counting the problem;
All unary operators, trinocular operators, and assignment operators are right-associative, and the other operators are left-associative;
!a++;
If the operation is from left to right,!a first gets a Boolean type, (!a) + + finally gets a numeric type;
If it is a right-to-left operation, a++ first gets a numeric type! (a++) to obtain a Boolean type;
The logical non (!) and increment operator (+ +) are unary operators that, when the two unary operators are present in one line of code, operate from right to left, so that the result of a Boolean type is finally obtained;
x = a? B:c? D:e? F:g;
The trinocular operator is also right-associative, right-to-left arithmetic, equivalent to
x = a? B: (c. D: (E. f:g));
var a = b = c = D;
Even the assignment value, also from right to left operation, first take the value of D, assign it to C, and then assign the value of C to B, and then assign the value of B to A;
Order of Operations
What do you do when an expression contains an expression?
A = 1= + + + + A;console.log (A, b ); // 2 3
This situation JS is always run from left to right;
- First JS will first calculate B, get B;
- Then the a++ is computed and the result is 1, but the value of a is changed to 2 after the process is finished.
- The third step calculates the back of a, gets 2;
- The fourth step calculates 1+2, obtains 3;
- Assign a value of 3 to B;
var a = 1= a + + + + A; // 4
- First calculate a++, get 1, then a value becomes 2;
- Then calculate ++a, get 3;
- 1+3 get 4;
- Assign a value of 4 to B;
Unary Plus, unary minus, increment operator, decrement operator
These four operators are valid for all data types;
- Unary Plus, unary minus is actually converting any data type into a numeric type (because all data types can be converted to numbers);
- Increment operator, decrement operator converts any data type first to a number, and then adds one or minus one operation;
But when you decrement a decimal, the result may be different from what you think.
var a = 1.1; --a; // 0.10000000000000009
Therefore, we must first convert the decimal number to an integer and then calculate the result and convert it back to decimals;
Bitwise non-
Just know a bit, a bitwise non-operation of a number, is to take a negative value minus one; For example, a bitwise non-operation of 10 results in 11;
Bitwise-AND
Can be used to judge the parity of numbers;
function even_or_odd (number) { return (number & 1)? "ODD": "Even";}
- The odd number is converted to binary and the last digit is 1, so the bitwise AND operation of 1 will return 1;
- Even conversion to binary the last digit is 0, so the bitwise AND operation of 1 will return 0;
Logical Non-
Convert any data type to Boolean type, then reverse;
Unary operators in the form of characters
typeof: You can enclose the expression it wants to manipulate in parentheses, but don't mistake it as a function, the parentheses are just handy to let people see the arithmetic logic;
void: Returns a undefined no matter what the expression is;
Delete: The property used to delete the object;
Instanceof: Used to judge the constructor of an object, but it is not reliable and is not used much;
———— finishing from Shahi teacher's Front end Audio tutorial # accompany you to read books # (Himalaya)
A unary operator in JS