If you are a C, C++, or Java programmer, then the JavaScript operators will almost all be already familiar to you. Table 4.1 summarizes the operators, and you can refer to this table for reference. In the table, the column labeled P gives the operator precedence, and the column labeled A gives the operator associativity, which can be L (left-to-right) or R (right-to-left).
If you do not already know C, C++, or Java, the sections that follow the table explain how to interpret the table and explain what each of the operators does.
P | A | Operator | Operand Type(s) | Operation Performed |
---|---|---|---|---|
0 | L | . | object, property | property access |
L | [] | array, integer | array index | |
L | () | function, args | function call | |
1 | R | ++ | number |
pre-or-post increment (unary) |
R | -- | number |
pre-or-post decrement (unary) |
|
R | - | number |
unary minus (negation) |
|
R | ~ | integer |
bitwise complement (unary) |
|
R | ! | Boolean |
logical complement (unary) |
|
R | typeof | any | return data type (unary) | |
R | new | constructor call | create new object (unary) | |
R | void | any | return undefined value (unary) | |
2 | L | *, /, % | numbers |
multiplication, division, remainder |
3 | L | +, - | numbers | addition, subtraction |
L | + | strings | string concatenation | |
4 | L | << | integers | left shift |
L | >> | integers |
right shift with sign-extension |
|
L | >>> | integers |
right shift with zero extension |
|
5 | L | <, <= | numbers or strings |
less than, less than or equal |
L | >, >= | numbers or strings |
greater than, greater than or equal |
|
6 | L | == | primitive types |
equal (have identical values) |
L | != | primitive types |
not equal (have different values) |
|
L | == | reference types |
equal (refer to same object) |
|
L | != | reference types |
not equal (refer to different objects) |
|
7 | L | & | integers | bitwise AND |
8 | L | ^ | integers | bitwise XOR |
9 | L | | | integers | bitwise OR |
10 | L | && | Booleans | logical AND |
11 | L | || | Booleans | logical OR |
12 | R | ?: | Boolean, any, any |
conditional (ternary) operator |
13 | R | = | variable, any | assignment |
R |
*=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |= |
variable, any | assignment with operation | |
14 | L | , | any | multiple evaluation |
In general, there are three types of operators. Most JavaScript operators, like the + operator that we saw in the previous section, are binary operators that combine two expressions into a single, more complex expression. That is, they operate on two operands. JavaScript also supports a number of unary operators, which convert a single expression into a single more complex expression. The - operator in the expression -3 is a unary operator which performs the operation of negation on the operand 3. Finally, JavaScript supports one ternary operator, ?:, which combines the value of three expressions into a single expression.
When constructing JavaScript expressions, you must pay attention to the data types that are being passed to operators, and to the data types that are returned. Different operators expect their operands' expressions to evaluate to values of a certain data type. For example, it is not possible to multiply strings, so the expression "a" * "b" is not legal in JavaScript. Note, however, that JavaScript tries to convert expressions to the appropriate type whenever possible, so the expression "3" * "5" is legal. Its value is the number 15, not the string "15".
Furthermore, some operators behave differently depending on the type of the operands. Most notably, the + operator adds numeric operands but concatenates string operands. And if passed one string and one number, it converts the number to a string and concatenates the two resulting strings. For example, '1' + 0 yields the string '10'.
Finally, note that operators do not always return the same type as their operands. The comparison operators (less than, equal to, greater than, etc.) take operands of various types, but when comparison expressions are evaluated, they always return a Boolean result that indicates whether the comparison is true or not. For example, the expression a < 3 returns true if the value of variable a is in fact less than 3. As we'll see, the Boolean values returned by comparison operators are used in if statements, while loops, and for loops--JavaScript statements that control the execution of a program based on the results of evaluating expressions that contain comparison operators.
In Table 4.1 the column labeled P specifies the precedence of each operator. Operator precedence controls the order in which operations are performed. Operators with a lower number in the P column are performed before those with a higher number. Somewhat confusingly, we say that operators that are performed first (with a lower P number) have higher precedence.
Consider the following expression:
w =index.html x + y*z;
w =index.html (x + y)*z;
In Table 4.1 the column labeled A specifies the associativity of the operator. A value of L specifies left-to-right associativity, and a value of R specifies right-to-left associativity. The associativity of an operator specifies the order in which operations of the same precedence are performed. Left-to-right associativity means that operations are performed from left to right. For example:
w =index.html x + y + z;
w =index.html ((x + y) + z);
x =index.html ~-~y; w = x = y = z; q = a?b:c?d:e?f:g;
x =index.html ~(-(~y)); w = (x = (y = z)); q = a?b:(c?d:(e?f:g));