EXPRESSIONS AND OPERATORS IN JAVASCRIPT


Expressions and Operators

Expressions

An expression is any valid set of literals, variables, operators and expresions that evaluates to a single value. This value can be a number, a string or a logical value.

Operators

JavaScript has assignment, comparision, arithmetic, bitwise, logical, string and special operators. There are binary and unary operations. Binary operations require two operands and one operator, and the order is:

operand1 operator operand2

Unary operations require a single operand, before or after the operator

operator operand

or

operand operator

JavaScript has also one ternary operation: the conditional operation, and it requires three operands.

Assignment Operators

An assignment operator assigns its left operand with the value of the right operand. The assignement operator is equal (‘=’). The shorthand operators exist for standard operations, assigning a new value to a variable based in its old value. Here’s a table representing the shorthand formats available in JavaScript.

Shorthand Operator Meaning Description
x += y x = x + y Adds x with y and sets x with the result
x -= y x = x – y Subtracts y to x and sets x with the result
x *= y x = x * y Multiplies x by y and sets x with the result
x /= y x = x / y Divides x by y and sets x with the result
x %= y x = x % y Sets x with the integer reminder of dividing x by y
x <<= y x = x << y Shifts x in binary representation y bits to left, shifting in zeros from right, and sets x with the result

 

x >>= y x = x >> y Shifts x in binary representation y bits to the right, discarding bits shifted off, and sets x with the result
x >>>= y x = x >>> y Shifts x in binary representation y bits to the right, discarding bits shifted off, shifting in zeros from the left, and sets x with the result
x &= y x = x & y Bitwise AND between x and y and sets x with the result
x ^= y x = x ^ y Bitwise XOR between x and y and sets x with the result
x |= y x = x | y Bitwise OR between x and y and sets x with the result

 

 

Arithmetic operator

Arithmetic operators include the familiar addition (+), subtraction (-), multiplication (*) and division (/) operations. In addition there is the modulus operator (%) which gives the remainder left over from a division operation. Let us look at some examples:

 

For the example consider a=12 and b=7

Operator Description Examples
+, -, *, / Standard arithmetic operators b-a returns -5
% Modulus. Returns the integer reminder of dividing a by b a % b returns 5
25 % a returns 1
++ Increments its unique operand by one. If it is used as prefix, the operand value and the return value are incremented. If it is used as a postfix, returns the value of its operand before adding one a++ and ++a set a to 13, but in the first case it returns 12 and in the second case it returns 13
Decrements its unique operand by one. If it is used as prefix, the operand value and the return value are decremented. If it is used as a postfix, returns the value of its operand before subtracing one b– and b++ set b to 6, but in the first case it returns 5 and in the second case it returns 6
Negation. Returns the negation of its operand -a returns -12

 

Comparision Operator

This operator compares two operands and return a logical value based on the comparision result. The operands can be numerical or strings: when using strings, the comparision is based on the lexicographical ordering. There are only two values possible: true or false. The following table describes these operators:

For the examples consider x = 4 and y = 5

Operator Description Examples
== Equal. Returns true if both  operands are equal x == y returns false
!= Not equal. Returns true if both operands are not equal x != y returns true
> Greater than. Returns true if the left operand is greater than the right operand x > y returns false
>= Greater or equal than. Returns true if the left operand is greater or equal than the right operand x >= y returns false
< Less than. Return true if left operand is les than right operand x < y returns true
<= Less or equal than. Returns true if left operand is less or equal than right operand. x <= y returns true

 

Bitwise Operators

These operators work with the binary representation of each of its operands, but they always return standard JavaScript numerical values.

For the example consider a=30 (11110), b=87(1010111) and c=3

Operator Description Examples
& AND.Returns a one in each bit position, if bits of both operands are ones. Returns a zero for all the other cases a & b returns 22 (10110)
| OR.Returns a zero in each bit position, if bits of both operands are zeros. Returns a one for all the other cases a | b returns 95 (1011111)
^ XOR. Returns a one in each bit position if bits of both operands are different. Returns a zero for all the other cases a ^ b returns 73 (1001001)
<< LEFT SHIFT. Shifts the first operand in binary representation by the number of bits refered by the second operand to the left, shifting in zeros from the right. a << c returns 240 (11110000)
>> SIGN-PROPAGATING RIGHT SHIFT. Shifts the first operand in binary representation by the number of bits refered by the second operand to the right, discarding bits shifted off. b >> c returns 10 (1010)
>>> ZERO-FILL RIGHT SHIFT. Shifts the first operand in binary representation by the number of bits refered by the second operand to the right, discarding the bits shifted off and shifting in zeros from the left. b >>> c returns 10 (1010)

String Operators

Comparison operators and the concatenation operator (+) can be used on string values. The concatenation operator returns a string which is the result of the union between the two operands (at least one of them must be a string.)

Operator Precedence

When evaluating an expression, there is a precedence order of the operators involved. There are only one pair of symbols which override any operator order: the parentheses.

Operator type Operators
assignment = += -= *= /= %= <<= >>= >>>= &= ^= |=
conditional ?:
logical-or ||
logical-and &&
bitwise-or |
bitwise-xor ^
bitwise-and &
equality == !=
relational < <= > >=
bitwise-shift << >> >>>
addiction/subtraction + –
multiply/divide * / %
negation/increment ! ~ – ++ __ typeof void
call. member () []

Some Special Operators

The conditional operator is the only operator in JavaScript that takes three operands. The syntax for this operator is:

(condition) ? val1: val2

If condition is true, the operator has the value of val1. Otherwise it has the value of val2. Here’s an example fot this operator:

status = (age>=18) ? “adult” : “minor”

The comma operator (,) simply evaluates both of its operands and returns the value of the second operand. This operator is primarily used inside a for loop, to allow multiple variables to be updated each time through the loop. Suppose you want to create a loop using the comma operator:

for (var i=0, j=5; i<=5; i++, j-=2)
document.writeln(“(“+i+”,”+j+”)”)

and as output we’ll have:

(0,5)
(1,3)
(2,1)
(3,-1)
(4,-3)
(5,-5)