JavaScript: The Definitive GuideJavaScript: The Definitive GuideSearch this book

5.2. Operator Overview

If you are a C, C++, or Java programmer, most of the JavaScript operators should already be familiar to you. Table 5-1 summarizes the operators; you can refer to this table for reference. Note that most operators are represented by punctuation characters such as + and =. Some, however, are represented by keywords such as delete and instanceof. Keyword operators are regular operators, just like those expressed with punctuation; they are simply expressed using a more readable and less succinct syntax.

In this 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 understand precedence and associativity, the subsections that follow the table explain these concepts. The operators themselves are documented following that discussion.

Table 5-1. JavaScript operators

P

A

Operator

Operand type(s)

Operation performed

15

L

.

object, identifier

Property access

 

L

[]

array, integer

Array index

 

L

( )

function, arguments

Function call

 

R

new

constructor call

Create new object

14

R

++

lvalue

Pre- or post-increment (unary)

 

R

--

lvalue

Pre- or post-decrement (unary)

 

R

-

number

Unary minus (negation)

 

R

+

number

Unary plus (no-op)

 

R

~

integer

Bitwise complement (unary)

 

R

!

boolean

Logical complement (unary)

 

R

delete

lvalue

Undefine a property (unary)

 

R

typeof

any

Return data type (unary)

 

R

void

any

Return undefined value (unary)

13

L

*, /, %

numbers

Multiplication, division, remainder

12

L

+, -

numbers

Addition, subtraction

 

L

+

strings

String concatenation

11

L

<<

integers

Left shift

 

L

>>

integers

Right shift with sign-extension

 

L

>>>

integers

Right shift with zero extension

10

L

<, <=

numbers or strings

Less than, less than or equal

 

L

>, >=

numbers or strings

Greater than, greater than or equal

 

L

instanceof

object, constructor

Check object type

 

L

in

string, object

Check whether property exists

9

L

==

any

Test for equality

 

L

!=

any

Test for inequality

 

L

===

any

Test for identity

 

L

!==

any

Test for non-identity

8

L

&

integers

Bitwise AND

7

L

^

integers

Bitwise XOR

6

L

|

integers

Bitwise OR

5

L

&&

booleans

Logical AND

4

L

||

booleans

Logical OR

3

R

?:

boolean, any, any

Conditional operator (3 operands)

2

R

=

lvalue, any

Assignment

 

R

*=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |=

lvalue, any

Assignment with operation

1

L

,

any

Multiple evaluation

5.2.1. Number of Operands

Operators can be categorized based on the number of operands they expect. Most JavaScript operators, like the + operator we saw earlier, 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 that performs the operation of negation on the operand 3. Finally, JavaScript supports one ternary operator, the conditional operator ?:, which combines the value of three expressions into a single expression.

5.2.2. Type of Operands

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". We'll consider JavaScript type conversions in detail in Section 11.1.

Furthermore, some operators behave differently depending on the type of the operands. Most notably, the + operator adds numeric operands but concatenates string operands. Also, 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".

Notice that the assignment operators, as well as a few other operators, expect their lefthand expressions to be lvalues. lvalue is a historical term that means "an expression that can legally appear on the lefthand side of an assignment expression." In JavaScript, variables, properties of objects, and elements of arrays are lvalues. The ECMAScript specification allows built-in functions to return lvalues but does not define any built-in functions that behave that way.

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.

5.2.3. Operator Precedence

In Table 5-1, the column labeled "P" specifies the precedence of each operator. Operator precedence controls the order in which operations are performed. Operators with higher numbers in the "P" column are performed before those with lower numbers.

Consider the following expression:

w = x + y*z; 

The multiplication operator * has a higher precedence than the addition operator +, so the multiplication is performed before the addition. Furthermore, the assignment operator = has the lowest precedence, so the assignment is performed after all the operations on the righthand side are completed.

Operator precedence can be overridden with the explicit use of parentheses. To force the addition in the previous example to be performed first, we would write:

w = (x + y)*z; 

In practice, if you are at all unsure about the precedence of your operators, the simplest thing is to use parentheses to make the evaluation order explicit. The only rules that are important to know are these: multiplication and division are performed before addition and subtraction, and assignment has very low precedence and is almost always performed last.

5.2.4. Operator Associativity

In Table 5-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, the addition operator has left-to-right associativity, so:

w = x + y + z; 

is the same as:

w = ((x + y) + z); 

On the other hand, the following (almost nonsensical) expressions:

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

are equivalent to:

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

because the unary, assignment, and ternary conditional operators have right-to-left associativity.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.