EXPRESSIONS AND OPERATORS


EXPRESSIONS

An expression  is any valid set of literals, variables, operators, and expressions that evaluates to a single numerical, textual, or logical value. Conceptually, there are two types of expressions: those that assign a value to a variable and those that simply have a value. For example, the expression

x = 7

is an expression that assigns x the value 7. This expression itself evaluates to 7. Such expressions use assignment operators. On the other hand, the expression

3 + 4

simply evaluates to 7; it does not perform an assignment. The operators used in such expressions are referred to simply as operators.


ASSIGNMENT OPERATORS (=, +=, -=, *=, /=)

An assignment operator  assigns a value to its left operand based on the value of its right operand. The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x.

The other operators are shorthand for standard arithmetic operations as follows:

There are additional assignment operators for bitwise operations:


OPERATORS

JavaScript supports arithmetic, string, and logical operators. There are both binary  and unary  operators. A binary operator requires two operands, one before the operator and one after the operator:

operand1 operator operand2

For example, 3 + 4 or x * y

A unary operator requires a single operand, either before or after the operator:

operator operand

or

operand operator

For example x++ or ++x.


ARITHMETIC OPERATORS

Arithmetic operators  take numerical values (either literals or variables) as their operands and return a single numerical value.

Standard Binary Arithmetic Operators

The standard arithmetic operators are:

These operators work in the standard way.

Modulus (%)

Usage: var1 % var2

The modulus operator  returns the first operand modulo the second operand, that is, var1  modulo var2, in the statement above, where var1  and var2  are variables. The modulo function is the remainder of integrally dividing var1  by var2. For example, 12 % 5 returns 2.

BITWISE OPERATORS

The bitwise operators are:

The binary bitwise operators work conceptually as follows:

SHIFT OPERATORS

The shift operators  take two operands: The first is a quantity to be shifted and the second specifies the number of bit positions by which the first operand is to be shifted. The direction of the shift operation is controlled by the operator used.

Shift operators convert their operands to 32-bit integers and return a result of the same type as the left operator.

Left Shift (<)

This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

Example TBD.

Sign-Propagating Right Shift (>>)

This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left.

Example TBD.

Zero-Fill Right Shift (>>>)

This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left.

Example TBD.

UNARY ARITHMETIC OPERATORS

Unary arithmetic operators  take one numerical value as an operand and return a numerical value.

Increment (++)

The increment operator is used as follows:

var++ or ++var

The increment operator does two things: It increments its operand, and it returns a value. If used postfix (for example, x++), then it returns the value before incrementing. If used prefix (for example, ++x), then it returns the value after incrementing.

For example, if x is 3, then the statement

y = x++

increments x to 4 and sets y to 3.

If x is 3, then the statement

y = ++x

increments x to 4 and sets y to 4.

Decrement (--)

The decrement operator is used as follows:

var-- or --var

The decrement operator does two things: It decrements its operand, and it returns a value. If used postfix (for example, x--), then it returns the value before decrementing. If used prefix (for example, --x), then it returns the value after decrementing.

For example, if x is 3, then the statement

y = x--

decrements x to 2 and sets y to 3.

If x is 3, then the statement

y = --x

decrements x to 2 and sets y to 2.

Unary Negation (-)

The unary negation operator must precede its operand. It negates its operand. For example,

x = -x

negates the value of x; that is, if x were 3, it would become -3.

LOGICAL OPERATORS

Logical operators  require logical (Boolean) values as operands. They return a logical value. Logical values are true and false.

And (&&)

Usage: expr1 && expr2

The logical "and" operator returns true if both logical expressions expr1 and expr2 are true. Otherwise, it returns false.

Or (||)

Usage: expr1 || expr2

The logical "or" operator returns true if either logical expression expr1 or expr2 is true. If both expr1 and expr2 are false, then it returns false.

A complex logical expression involving the || operator is always tested for "short circuit" evaluation. What this means is that if one of the operands evaluates immediately to true, and the other is still undetermined (because it requires further evaluation), the expression immediately evaluates to true, since it doesn't matter what the undetermined part is; anything OR true is true.

Not (!)

Usage: !expr

The logical "not" operator negates its operand expression expr. That is, if expr is true, it returns false, and if expr is false, then it returns true.

Short-Circuit Evaluation

As logical expressions are evaluated left to right, they are tested for possible "short circuit" evaluation. What this means is that the following rule is applied:

This is possible because anything AND false is always false, and any value OR true is always true.

COMPARISON OPERATORS (= =, >, >=, <, <=, !=)

A comparison operator  compares its operands and returns a logical value based on whether the comparison is true or not. The operands may be numerical or string values. When used on string values, the comparisons are based on the standard lexicographical ordering.

The operators are:

STRING OPERATORS

In addition to the comparison operators, which may be used on string values, the concatenation operator  (+) concatenates two string values together, returning another string that is the union of the two operand strings. For example,

"my " + "string"

returns the string

"my string"

The shorthand assignment operator += can also be used to concatenate strings. For example, if the variable mystring is a string that has the value "alpha", then the expression

mystring += "two"

evaluates to "onetwo" and assigns this value to mystring.

OPERATOR PRECEDENCE

The precedence  of operators determines the order they are applied when evaluating an expression. You can override operator precedence by using parentheses.

The precedence of operators, from highest to lowest is as follows:

comma ,
assignment = += -= *= /= %= <<= >>= >>>= &= ^= |=
conditional ?:
logical-or ||
logical-and &&
bitwise-or |
bitwise-xor ^
bitwise-and &
equality == !=
relational < <= > >=
shift <<>> >>>
addition/subtraction + -
multiply/divide * / %
negation/increment ! ~ - ++ --
call, member () [] .