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 51 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
(lefttoright) or R (righttoleft). 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 51. 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 postincrement (unary)


R



lvalue

Pre or postdecrement (unary)


R



number

Unary minus (negation)


R

+

number

Unary plus (noop)


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 signextension


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 nonidentity

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.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 builtin functions
to return lvalues but does not define any builtin 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. Expressions and Operators   5.3. Arithmetic Operators 
Copyright © 2003 O'Reilly & Associates. All rights reserved.

