Home > Web Front-end > JS Tutorial > Detailed explanation of operator definition and usage in JavaScript basic course

Detailed explanation of operator definition and usage in JavaScript basic course

伊谢尔伦
Release: 2017-07-19 10:07:36
Original
1260 people have browsed it

Operators in JavaScript are used for table expressions, comparison expressions, logical expressions, assignment expressions, etc.
It should be noted that most operators are represented by punctuation marks, such as delete and instanceof. Whether it is a keyword operator or a symbolic operator, the operators represented are all regular operators, and their syntax is very concise and comprehensive.
Sorted by the priority of the subscript operator, the priority of the previous operator is higher than the priority of the later operator. Operators separated by horizontal scalars have different precedence.
A represents the associativity of the operator.
L from left to right or R (from right to left)
The list of header N indicates the number of operands.
The type indicates the type of expected operands and the result type of the operator (after the "→" symbol)

i. The number of operands

The operator can be operated by Classify the number of numbers.

Most operators in JavaScript are binary operators, combining two expressions into a slightly more complex expression.
Javascript also supports some unary operators, which convert one expression into another slightly more complex expression. The "-" operator in the expression -x is a unary operator. is to take the negative value of x.
Javascript supports a ternary operator: the conditional judgment operator "?:", which combines three expressions into one expression

ii. Operand type and result type

Some operators can be used on any data type, but they are still expected to operate on data of a specified type.

iii. lvalue

The assignment operator and a few other operators in the table expect their operands to be of type lval, lvalue being an archaic term. It means "an expression can only appear on the left side of an assignment operator". In JavaScript, variables, object properties, and array elements are all lvalues. The ECMAScript specification allows scoped built-in functions to return an lvalue, but defined functions cannot.

iii.Priority of operators

In the above table, the operators shown are sorted from high to low by priority, with a group of operators within each horizontal dividing line have the same priority. Operator precedence controls the order in which operators are executed. Operators with higher precedence (top of the table) are always executed before operators with lower precedence (bottom of the table).

Look at the following expression

w=x+y*z;
The multiplication operator "*" has a higher priority than the addition "+", so the multiplication is executed first. Then, since the assignment operator "=" has the lowest precedence. Therefore, the assignment operation is performed after the expression on the right-hand side has been evaluated.

The precedence of the operator can be written using parentheses. The above expression can be written like this.

w = (x + y) * z;
It should be noted that the priority of attribute access expressions and call expressions is higher than all operators in the table.

typeof my.Function[x](y)
Although typeof is one of the highest precedence operators, typeof is also executed after two property accesses and function calls.

In fact, if you are really not sure about the precedence of the operators you are using, the easiest way is to use parentheses to force the order of operations. There are some important rules to remember: multiplication and division are higher than addition and subtraction, and assignment operations have very low priority and are usually performed last.

iiiiii. Associativity of operators

In the table in this section, the column titled A describes the associativity of operators. L means to combine from left to right, R means to combine from right to left. Tuberculity specifies the order of operations in multiple operator expressions with the same precedence.

For example, subtraction operations are performed associatively from left to right.

w = x - y - z
Copy after login

is the same as this code:

w = ((x - y) - z)
Copy after login

Conversely, the following expression:

x = ~-y;
w = x = y = z;
q=a?b:c?d:e?f:g;
Copy after login

is exactly the same as this code

x=~(-y);
w=(x=(y=z));
q=a?b:(c?d:(e?f:g))
Copy after login

Because one yuan Operators, assignments, and ternary conditional operators all have right-to-left associativity.

iiiiiii. Order of operations

The precedence and associativity of operators specify their order of operation in the assignment expression, but do not specify the operation during the calculation of the subexpression. order. JavaScript always calculates expressions strictly from left to right, for example:

In the expression w=x+y*z, the expression w will be calculated first, and then x, y and z will be calculated. Then, the value of y is multiplied by the value of z, and the value of x is added. Finally, the variable or attribute pointed to by its expression w. Adding parentheses to an expression changes the relationship between multiplication, addition, and assignment operations. But the order from left to right will not change.

The above is the detailed content of Detailed explanation of operator definition and usage in JavaScript basic course. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template