Appendix D

JavaScript Operators


CONTENTS

JavaScript expressions use special symbols called operators to perform arithmetic and string manipulation. This appendix explains what the various operators do, as well as operator precedence (which operator is used first).

Operators

Operators may be unary, binary, or ternary. A unary operator uses a single value, or operand. A binary operator uses two operands. A ternary operator uses three operands.

Addition Operator

The addition operator is a binary operator. The syntax is

operand1 + operand2

The value of this expression is the arithmetic sum of operand1 and operand2.

Example:

var tmp = 4 + 7.3;

In this example, operand1 is 4 and operand2 is 7.3. tmp is set to 11.3, the arithmetic sum of operand1 (4) and operand2 (7.3).

Assignment Operator

The assignment operator is a binary operator. The syntax is

operand1 = operand2

The value of this expression is that of operand2. The value of operand2 is copied to operand1.

Example:

var tmp = 2;

In this example, operand1 is tmp and operand2 is 2. operand1 (tmp) is set to the value of operand2 (2).

Bitwise AND Operator

The bitwise AND operator is a binary operator. The syntax is

operand1 & operand2

You obtain the result of this expression by treating operand1, operand2, and the result as sets of 32 bits. Each bit of the result is the corresponding bits of operand1 and operand2 ANDed together. The result of AND on two bits is 1 if both bits are 1, and 0 otherwise.

Example:

var tmp = 79 & 31;

In this example, operand1 is 79 (00000000000000000000000001001111) and operand2 is 31 (00000000000000000000000000011111). tmp is set to 15 (00000000000000000000000000001111).

Bitwise Exclusive-Or (XOR) Operator

The bitwise exclusive-or (XOR) operator is a binary operator. The syntax is

operand1 ^ operand2

You obtain the result of this expression by treating operand1, operand2, and the result as sets of 32 bits. Each bit of the result is the corresponding bits of operand1 and operand2 XORed together. The result of XOR on two bits is 1 if only one of the bits is 1, and 0 if both bits are identical.

Example:

var tmp = 79 ^ 96;

In this example, operand1 is 79 (00000000000000000000000001001111) and operand2 is 96 (00000000000000000000000001100000). tmp is set to 47 (00000000000000000000000000101111).

Bitwise OR Operator

The bitwise OR operator is a binary operator. The syntax is

operand1 | operand2

You obtain the result of this expression by treating operand1, operand2, and the result as sets of 32 bits. Each bit of the result is the corresponding bits of operand1 and operand2 ORed together. The result of OR on two bits is 1 if either bit is 1, and 0 if both bits are 0.

Example:

var tmp = 79 | 96;

In this example, operand1 is 79 (00000000000000000000000001001111) and operand2 is 96 (00000000000000000000000001100000). tmp is set to 111 (00000000000000000000000001101111).

Bitwise Negate Operator

The bitwise negate operator is a unary operator. The syntax is

~ operand1

You obtain the result of this expression by treating operand1 and the result as sets of 32 bits. Each bit of the result is the inverse of the corresponding bit of operand1.

Example:

var tmp = ~79;

In this example, operand1 is 79 (00000000000000000000000001001111). tmp is set to -80 (11111111111111111111111110110000).

Concatenation Operator

The concatenation operator is binary. The syntax is

operand1 + operand2

The result of this expression is a string that concatenates the contents of the string operand1 and the string operand2.

Example:

var tmp = "Hello" + " world";

In this example, operand1 is "Hello" and operand2 is " world". The result is "Hello world".

Conditional Operator

The conditional operator is the only ternary operator. The syntax is

operand1 ? operand2 : operand3

The value of this expression depends on operand1. If operand1 evaluates as true, the value is operand2. Otherwise, the value is operand3.

Example:

var tmp = (x == 1) ? 7.2 : foo();

In this example, operand1 is (x == 1), operand2 is 7.2, and operand3 is foo(). If operand1 is true (x is equal to 1), tmp is set to operand2 (7.2). If operand1 is false (x is not equal to 1), tmp is set to operand3 (the return value of foo()).

Division Operator

The division operator is binary. The syntax is

operand1 / operand2

The value of this expression is the value of operand1 divided by operand2. operand2 cannot be equal to 0.

Example:

var tmp = 24 / 5;

In this example, operand1 is 24 and operand2 is 5. tmp is set to operand1 divided by operand2, which is 4.8.

Equality Operator

The equality operator is binary. The syntax is

operand1 == operand2

The value of this expression is true if operand1 is equal to operand2, and false if operand1 is not equal to operand2.

Example:

var tmp = (foo == bar);

In this example, operand1 is foo and operand2 is bar. If foo and bar are equal, tmp is set to true. Otherwise, tmp is set to false.

Greater Than Operator

The greater than operator is binary. The syntax is

operand1 > operand2

The value of this expression is true if operand1 is greater than operand2, and false if operand1 is less than or equal to operand2.

Example:

var tmp = (foo > bar);

In this example, operand1 is foo and operand2 is bar. If foo is greater than bar, tmp is set to true. Otherwise, tmp is set to false.

Greater Than or Equal Operator

The greater than or equal operator is binary. The syntax is

operand1 >= operand2

The value of this expression is true if operand1 is greater than or equal to operand2, and false if operand1 is less than operand2.

Example:

var tmp = (foo >= bar);

In this example, operand1 is foo and operand2 is bar. If foo is greater than or equal to bar, tmp is set to true. Otherwise, tmp is set to false.

Inequality Operator

The inequality operator is binary. The syntax is

operand1 != operand2

The value of this expression is true if operand1 is not equal to operand2, and false if operand1 is equal to operand2.

Example:

var tmp = (foo != bar);

In this example, operand1 is foo and operand2 is bar. If foo and bar are not equal, tmp is set to true. Otherwise, tmp is set to false.

Less Than Operator

The less than operator is binary. The syntax is

operand1 < operand2

The value of this expression is true if operand1 is less than operand2, and false if operand1 is greater than or equal to operand2.

Example:

var tmp = (foo < bar);

In this example, operand1 is foo and operand2 is bar. If foo is less than bar, tmp is set to true. Otherwise, tmp is set to false.

Less Than or Equal Operator

The less than or equal operator is binary. The syntax is

operand1 <= operand2

The value of this expression is true if operand1 is less than or equal to operand2, and false if operand1 is greater than operand2.

Example:

var tmp = (foo <= bar);

In this example, operand1 is foo and operand2 is bar. If foo is less than or equal to bar, tmp is set to true. Otherwise, tmp is set to false.

Logical AND Operator

The logical AND operator is binary. The syntax is

operand1 && operand2

This expression is true if both operand1 and operand2 are true; otherwise, it's false.

Example:

var tmp = foo && bar;

In this example, operand1 is foo and operand2 is bar. tmp is set to true if both foo and bar are true, and false if either foo or bar is false.

Logical NOT Operator

The logical NOT operator is unary. The syntax is

! operand1

This expression is true if operand1 is false; otherwise, it's false.

Example:

var tmp = !bar;

In this example, operand1 is bar. tmp is set to true if bar is false, and false if bar is true.

Logical OR Operator

The logical OR operator is binary. The syntax is

operand1 || operand2

This expression is true if either operand1 or operand2 is true; otherwise, it's false.

Example:

var tmp = foo || bar;

In this example, operand1 is foo and operand2 is bar. tmp is set to true if either foo or bar is true, and false if both foo and bar are false.

Modulo Operator

The modulo operator is binary. The syntax is

operand1 % operand2

The value of this expression is the remainder of performing an integer division of operand1 by operand2. operand2 cannot be equal to 0. The absolute value of the result will be less than the absolute value of operand2, and the sign of the result will be the same as the sign of operand1.

Example:

var tmp = 12.1 % 3.7

In this example, operand1 is 12.1 and operand2 is 3.7. tmp is set to the remainder of dividing operand1 divided by operand2. 12.1 divided by 3.7 is 3 with a remainder of 1, so tmp is set to 1.

Multiplication Operator

The multiplication operator is binary. The syntax is

operand1 * operand2

The value of this expression is the value of operand1 multiplied by operand2.

Example:

var tmp = foo * bar;

In this example, operand1 is foo and operand2 is bar. tmp is set to the product of operand1 (foo) multiplied by operand2 (bar).

Negation Operator

The negation operator is unary. The syntax is

- operand1

The value of this expression is the value of operand1 subtracted from 0.

Example:

var tmp = -foo;

In this example, operand1 is foo. If operand1 (foo) is equal to 3, tmp is set to -3 (the value of foo subtracted from 0).

It is customary not to include spaces between the operator and the operand.

Post-Decrement Operator

The post-decrement operator is unary. The syntax is

operand1 --

The value of this expression is operand1. As a side effect, 1 is subtracted from operand1.

Example:

var tmp = foo--;

In this example, operand1 is foo. tmp is set to foo, and foo is set to foo - 1.

The result of this expression

foo = foo--;

is not defined.

It is customary not to include spaces between the operator and the operand.

Post-Increment Operator

The post-increment operator is unary. The syntax is

operand1 ++

The value of this expression is operand1. As a side effect, 1 is added to operand1.

Example:

var tmp = foo++;

In this example, operand1 is foo. tmp is set to foo, and foo is set to foo + 1.

The result of this expression:

foo = foo++;

is not defined.

It is customary not to include spaces between the operator and the operand.

Pre-Decrement Operator

The pre-decrement operator is unary. The syntax is

-- operand1

The value of this expression is operand1 minus 1. As a side effect, 1 is subtracted from operand1.

Example:

var tmp = --foo;

In this example, operand1 is foo. tmp is set to foo - 1, and foo is set to foo - 1.

The result of this expression:

foo = --foo;

is not defined.

It is customary not to include spaces between the operator and the operand.

Pre-Increment Operator

The pre-increment operator is unary. The syntax is

++ operand1

The value of this expression is 1 plus operand1. As a side effect, 1 is added to operand1.

Example:

var tmp = ++foo;

In this example, operand1 is foo. tmp is set to foo + 1, and foo is set to foo + 1.

The result of this expression:

foo = ++foo;

is not defined.

It is customary not to include spaces between the operator and the operand.

Shift Left Operator

The shift left operator is binary. The syntax is

operand1 << operand2

You obtain the result of the expression by treating operand1 as a set of 32 bits and shifting the bits left by the value in operand2. High-order bits are discarded, and the low-order bits are filled with 0's.

Example:

var tmp = 31 << 3;

In this example, operand1 is 31 (00000000000000000000000000011111) and operand2 is 3. tmp is set to 248 (00000000000000000000000011111000).

Sign-Propagating Shift Right Operator

The sign-propagating shift right operator is binary. The syntax is

operand1 >> operand2

You obtain the result of the expression by treating operand1 as a set of 32 bits and shifting the bits right by the value in operand2. High-order bits are copied from the highest-order bit (which dictates the sign of the value, hence sign-propagating), and low-order bits are discarded.

Example:

var tmp = -9 >> 2;

In this example, operand1 is -9 (11111111111111111111111111110111) and operand2 is 2. tmp is set to -3 (11111111111111111111111111111101).

Shorthand Assignment Operator

The shorthand assignment operator is a binary operator. The syntax is

operand1 op= operand2

The value of this expression is the value of operand1 op operand2, where op can be addition (+), bitwise and (&), bitwise exclusive-or (^), bitwise or (|), division (/), modulo (%), multiplication (*), shift left (<<), sign-propagating shift right (>>), subtraction(-), or zero-fill shift right (>>>). As a side effect, operand1 is also assigned the result of operand1 op operand1. This is equivalent to:

operand1 = operand1 op operand2

Example:

tmp += 2;

In this example, operand1 is tmp, op is +, and operand2 is 2. operand2 (2) is added to operand1 (tmp).

Subtraction Operator

The subtraction operator is binary. The syntax is

operand1 - operand2

The value of this expression is the value of operand2 subtracted from operand1.

Example:

var tmp = 14 - foo();

In this example, operand1 is 14 and operand2 is foo(). tmp is set to the value of operand2 (the return value of foo()) subtracted from operand1 (14).

Zero-Fill Shift Right Operator

The zero-fill shift right operator is binary. The syntax is

operand1 >>> operand2

You obtain the result of the expression by treating operand1 as a set of 32 bits and shifting the bits right by the value in operand2. High-order bits are filled with 0's (hence zero-fill), and low-order bits are discarded.

Example:

var tmp = -9 >>> 2;

In this example, operand1 is -9 (11111111111111111111111111110111) and operand2 is 2. tmp is set to 1,073,741,821 (00111111111111111111111111111101).

Order of precedence

The order of precedence determines the order in which the browser applies operators to operands when evaluating an expression. This determines how to evaluate an expression such as:

var tmp = 4 + 5 * 6;

If you assume that addition has higher priority than multiplication, tmp has the value 54 (4 + 5 * 6 = 9 * 6 = 54). If you assume that multiplication has higher priority than addition, tmp has the value 34 (4 + 5 * 6 = 4 + 30 = 34).

The order of precedence, from highest (perform first) to lowest (perform last) is

You can override the order of precedence by using parentheses to block off subexpressions that the browser must evaluate first.