JavaScript Operators (comparison, arithmetic, string, etc.)

The set of operators that JavaScript uses is, very similar to that of the C, C++, and Java languages. It provides a number of different ways of combining different values, both literals and variables, into expressions.

Unary or Binary – Some operators require two elements to participate in the operation, and are referred to as binary operators. The + operator is a binary operator. Other operators require only a single participant (operand), and are known as unary operators. The ++ operator, which adds 1 to its operand, is a unary operator.

JavaScript operators may be classified into the following groups:

Aritmetic Operators – The computational operators are addition (+), sub-traction and negation (-), division (/), multiplication (*), modulus (%), increment (++), and decrement (–). These operators are often used in performing arithmetic computations.

The first five computational operators have their standard mathematical meanings. They add, subtract, divide, or multiply two numeric quantities. If an integer is added to a floating-point number, the result is a floating-point number. The following four statements illustrate the use of these operators:

x = 4 + y;

y = 5.5 – z;

z = 10 / w;

w = 1.4e5 * v;

Note that division of integer quantities result in an integer result, so that if we had the value 4 in the third statement, z would get the value 2, not 2.5. Note also that the – operator may also be used as a unary operator to compute the negative of a numeric quantity:

n = -m;

This has exactly the same effect as if we had multiplied m by -1. The modulus operator (%) is used to compute the remainder from a division. Although it can be used with floating-point numbers, it is typically used with integers, so that 21 % 4 evaluates to 1. The modulus operator always gives a remainder that has the same sign as corresponding quotient, so that -21 % 4 evaluates to -1, not 3.

The increment and decrement operators add or subtract one from a number. Both these operators are unary and come in two forms: prefix and postfix. The expression ++x is the preincrement form of the ++ operator, while x++ is the postincrement form. For example

y = ++x;

z = x++;

If x has the value 10, y ends up with the value 11 while z has the value 10. The same rule applies to the decrement operator (–).

Logical Operators – They are used to carry out some form of test, or to combine the results of more than one such test. They are often referred to as conditional operators. The logical operators that perform a test of some sort are the equality/inequality operator (== and !=), the comparison operators (<, <=, >, and =>), and the logical negation operator (!). The operators that combine logical values are logical AND (&&) and logical OR (||). Finally, the conditional operator (?) and the comma operator (,) are also combining operators, although they are only vaguely logical operators.

Equality Operators  – The binary equality (==) and inequality (!=) operators are used to test if two quantities are the same or different. These operators are overloaded. On integers, they test for strict equality or inequality. On floating-point numbers, they test to see if the two quantities are equal within the precision of the underlying floating-point type. On strings, they test for exact equality-recall that case is significant in JavaScript strings. These operators all return a boolean value, either true or false.

Comparison Operators – The comparison operators (<, <=, > and >=) also operate on both numbers and strings. When they act on numbers they perform the usual arithmetic comparisons, yielding boolean values, as with the equality operators. When they act on strings they perform comparisons based on dictionary order, also known as lexicographic order.

The Negation Operator – The logical negation operator (!) is used to reverse the sense of a logical test. It converts true to false and false to true. If x < 15 is true then !(x < 15) is false, and vice versa. Note that ! may also be used with integer values, so that !0 is true, while !5 is false.

Boolean Logical Operators  The logical AND (&&) and OR (||) operators are the boolean operators. Both may be used to combine two or more conditions into a composite test. The logical AND of a set of conditions is true only if all of its component conditions are true. The logical OR of a set of conditions is true if any of its component conditions are true. Thus

( x < 17 ) && buttonPressed && ( z == “Meta” )

is true precisely when x is less than 17 and the boolean variable buttonPressed is true and z is exactly equal to the string “Meta.” Similarly,

( x < 17 ) || buttonPressed || ( z == “Meta” )

is true if one or more of the three conditions is true.

The conditional operator – It is the only ternary (3 operand) operator, used to select one of two possible alternatives based on a conditional test. The syntax for this operator is

( conditionthing ? truealt : falsealt )

If the conditionthing is true then the value of this expression is truealt; otherwise it is falsealt. Note that the colon (:) separating the true alternative from the false alternative is mandatory. This can be used to select an appropriate alternative and simplify code, as in this example:

printme = ( errorcode == 0 ? “OK” : “error” );

This expression makes the variable printme have the string value “OK” in case the variable errorcode is 0; otherwise, it is set to “error”. The question mark operator is often a fast way to select one of two choices when a control structure would be unnecessarily cumbersome.

Bitwise Operators – . The bitwise operators are used for to know the precise binary representation of values or it is absolutely essential to operate at the lowest possible level and deal with the individual bits of a particular value. JavaScript’s bitwise operators are listed below and all are binary, except for bitwise NOT, which is unary. Each operates on its operands one bit at a time.

Operator Name Symbol
Bitwise AND &
Bitwise OR |
Bitwise XOR ^
Bitwise Left Shift <<
Bitwise Signed Right Shift >>
Bitwise Unsigned Right Shift >>>
Bitwise NOT ~

Bitwise AND (&) examines each bit position in each of its operands. If both operands have a 1 bit in a given position, then that bit will also be set to 1 in the result. In all other cases, the output bit position is zero. For example, suppose x = 0x00001234 and y = 0x8000ABCD. Then z = x & y will have the value 0x00000204.

The unary bitwise NOT operator (~) changes each 0 bit in its operand to a 1 bit, and each 1 bit in its operand to a 0 bit. The bitwise NOT of x will have the value 0xFFFFEDCB:

    x     0000 0000 0000 0000 0001 0010 0011 0100
    ~x     1111 1111 1111 1111 1110 1101 1100 1011

While &, |, ^ and ~ operate on bits in place, the shift operators <<, >>, and >>> are used to move bits around. The left shift operator (<<) shifts a set of bits to the left by a specified number of positions, while both >> and >>> moves that set of bits to the right in two potentially different ways. For example, let us evaluate these three expressions:

xleft = x << 5;

ysright = y >> 3;

yusright = y >>> 3;

The first of these shifts each bit in x to the left five positions. Zero bits are tacked on at the right, while the bits that are shifted out at the left are lost when they exceed the overall 32-bit length. So the value of xleft must be 0x00024680. The signed right shift operator acts in almost the same way. Each bit of y is shifted to the right three positions. Bits on the right edge of y are lost as they are shifted out. However, rather than shifting in zeros at the left side of y, the most significant bit of y, which happens to be 1 in this case, is shifted in. The resulting value of ysright is 0xF0001579.

Assignment and Aggregate Operators  – You are well aware that it is used to assign the result of an expression or value on the right side of the = sign to the variable or lvalue on the left side of the = sign. Javascript can also combine the assignment operator with any of the binary arithmetic and

logical operators. The expression

Left OP= Right ;

is just a shorthand for the expression

Left = Left OP Right ;

where OP is any of the operators +, -, /, *, %, &, |, ^, <<, >>, or >>>. So, to add 7 to x, multiply y by 19.5, OR z with 0xAA7700, and perform an unsigned right shift of 10 bits on w you can write

x += 7;

y *= 19.5;

z |= 0xAA7700;

w >>>= 10;

Order of Evaluation  – It refers to the order of evaluation when many operators are used. The operator precedence table for JavaScript is

Operator Operator Use Operator Associativity Operator Precedence
() Method/function call, grouping Left to right Highest — 1
[] Array access Left to right 1
. Object property access Left to right 1
++ Increment Right to left 2
Decrement Right to left 2
Negation Right to left 2
! Logical NOT Right to left 2
~ Bitwise NOT Right to left 2
delete Removes array value or object property Right to left 2
new Creates an object Right to left 2
typeof Returns data type Right to left 2
void Specifies no value to return Right to left 2
/ Division Left to right 3
* Multiplication Left to right 3
% Modulus Left to right 3
+ Plus Left to right 4
+ String Concatenation Left to right 4
Subtraction Left to right 4
>> Bitwise right-shift Left to right 5
<< Bitwise left-shift Left to right 5
>, >= Greater than, greater than or equal to Left to right 6
<, <= Less than, less than or equal to Left to right 6
== Equality Left to right 7
!= Inequality Left to right 7
=== Identity operator — equal to (and same data type) Left to right 7
!== Non-identity operator — not equal to (or don’t have the same data type) Left to right 7
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical OR Left to right 12
?: Conditional branch Left to right 13
= Assignment Right to left 14
*=, /=, %=, +=,, -=, <<=, >>=, >>>=, &=, ^=, |= Assignment according to the preceding operator Right to left 14
, Multiple evaluation Left to right Lowest: 15

Back to Tutorial

Get industry recognized certification – Contact us

Menu