Operators are symbols, such as +, –, =, >, and <, that produce a result based on some rules. Data manipulated by an operator called operands; for example, 5 and 4 are operands in the expression 5 + 4. Operators and operands create expressions. An expression combines a group of values to make a new value, x = 5 + 4.
operators can be categorized by number of operands :
operators manipulate two operands, are called a binary operators.
If there is only one operand, the operator is called a unary operator.
If there are three operands, it is called a ternary operator.
The operands can be either strings, numbers, Booleans, or a combination of these.
Example :
operators can be categorized by number of operands :
operators manipulate two operands, are called a binary operators.
If there is only one operand, the operator is called a unary operator.
If there are three operands, it is called a ternary operator.
The operands can be either strings, numbers, Booleans, or a combination of these.
Precedence and Associativity
the following table is organized by operator precedence.
The operators listed first have higher precedence than those listed last.
with background color separating groups of operators at the same precedence level.
The column labeled A gives the operator associativity,
which can be L (left-to-right) or R (right-to-left),
and the column N specifies the number of operands
Operator | Operation | A | N | |
++ - - - + ~ ! delete typeof void | Pre- or post-increment Pre- or post-decrement Negate number Convert to number Invert bits Invert boolean value Remove a property Determine type of operand Return undefined value | R R R R R R R R R | 1 1 1 1 1 1 1 1 1 | |
*, /, % | Multiply, divide, remainder | L | 2 | |
+, - + | Add, subtract Concatenate strings | L L | 2 2 | |
<< >> >>> | Shift left Shift right with sign extension Shift right with zero extension | L L L | 2 2 2 | |
<, <=,>, >= <, <=,>, >= instanceof in | Compare in numeric order Compare in alphabetic order Test object class Test whether property exists | L L L L | 2 2 2 2 | |
= = != = = = != = | Test for equality Test for inequality Test for strict equality Test for strict inequality | L L L L | 2 2 2 2 | |
& | Compute bitwise AND | L | 2 | |
^ | Compute bitwise XOR | L | 2 | |
| | Compute bitwise OR | L | 2 | |
&& | Compute logical AND | L | 2 | |
|| | Compute logical OR | L | 2 | |
?: | Choose 2nd or 3rd operand | R | 3 | |
= *=, /=, %=, +=, -=, &=, ^=, |=, <<=, >>=, >>>= | Assign to a variable or property Operate and assign | R R | 2 2 | |
, | Discard 1st operand, return second | L | 2 |
Example :
The order of associativity is from left to right.
Multiplication and division are of a higher precedence than addition and subtraction, and addition and subtraction are of higher precedence than assignment.
To illustrate this, we'll use parentheses to group the operands as they are grouped by JavaScript. In fact, if you want to force precedence, use the parentheses around the expression to group the operands in the way you want them evaluated.
The following two examples produce the same result:
var r = 1 + 2 * 3 / 4; // 2.5
could be written
var r = (1 + ( ( 2 * 3 ) / 4)); // 2.5
The expression is evaluated and the result is assigned to variable r.
No comments:
Post a Comment