JS: Operators | JavaScript
JavaScript Operators
var sum = 1 + 2;
- The variable, identifier, constant or number used in the operation is called operand (e.g. 1 and 2)
- The symbol performing operation is called operator (e.g. +)
Operator is a symbol that performs a particular operation.
JavaScript operators are following types:
- Arithmetic operators
- Assignment operators
- Logical operators
- Comparision operators
- Bitwise operators
In this chapter considerx = 1
,y = 2
andz = 3
for upcoming examples
JavaScript Arithmetic operators
JavaScript arithmetic operators perform an arithmetic (mathematical or algebric) operation. Following are the JavaScript arithmetic operators in order of their precedence.
- ++
- --
- %
- *
- /
- +
- -
- **
JavaScript Assignment operators
- =
- +=
- -=
- *=
- /=
- **=
JavaScript Comparision operators
JavaScript Comparision operators perform a comparision operation.
- ==
- ===
- !=
- !==
- >
- <
- >=
-
JavaScript == equal operator
JavaScript == operator compares either two operands are equal.
JavaScript === operator
JavaScript != operator
JavaScript !== operator
JavaScript < operator
JavaScript > operator
JavaScript >= operator
JavaScript <= operator
JavaScript Comparision operators truth table
Operator Example Result == x == "1" true === x === "1" false != x != "1" false !== x !== "1" true > x > 1 false < x < 1 false >= x >= 1 true <= x <= 1 true JavaScript Logical operators
var x=1, y=2; if(x > y) console.log('Greater'); else if(x < y) console.log('Smaller'); else console.log('Equal');
JavaScript Logical or Relational operators compare logical results.
- !
- ||
- &&
JavaScript ! NOT operator
JavaScript || OR operator
JavaScript && AND operator
JavaScript Bitwise operators
JavaScript bitwise operators perform an the respective operation on 32-bit numbers . Following are the JavaScript arithmetic operators in order of their precedence.
- &
- |
- ~
- ^
- <<
- >>
- >>>
JavaScript Ternary Operator
The ternary (or conditional) operator is the alternate of
if
, works as a single statement.var s = x > y ? 'Greater' : x < y :'Smaller' : 'Equal'; console.log(s);
JavaScript Ternary operator is the only operator require three operands:- Condition (just like in
if
-else
statement) - Expression when condition is
true
- Expression when condition is
false
(just likeelse
statement)
Following are the properties of Ternary conditional operator:
- Takes three arguments
- Condition applied in a single statment
- Allows nested conditions
- Allows single statment for
true
&false
conditions, not block unlikeif
else
condition.
JavaScript operators precedence
Operators precedence means which operator will be computed first among other operators. Operator precedence can said as the operator priority given by JavaScript. Following is the table of JavaScript operators according to their precedence, high to low.
Operator Name Association Example () Parenthesis Grouping a*(c+d) . Object member access left-to-right arr.length [] Computed Member Access left-to-right arr[0] new object creation and constructor arguments not-applicable new Array(5,23,680) () Function call left-to-right parseInt("3") new object creation with empry constructor right-to-left new Array() ++,-- Postfix Increment, Postfix Decrement not-applicable x++,y-- ! ~ + - ++ -- typeof void delete Logical NOT, Bitwise NOT, Unary Plus, Unary Nevation, Prefix Increment, Prefix Decrement, typeof statement, void keyword, delete statement right-to-left !++x>--y ** Exponentiation (exponent/power) right-to-left % Remainder/Modulus left-to-right * / Product (Multiply/Divide) left-to-right + - Sum (Plus/Minus) left-to-right << >> >>> Bitwise Shift < <= > >= in instanceof Relational Operators, in keyword, instanceof keyword left-to-right == != === !== Equality, Inequality, Strict Equality and Strict Inequality Operators left-to-right & Bitwise AND left-to-right ^ Bitwise XOR left-to-right | Bitwise OR left-to-right && Logical AND left-to-right || Logical OR left-to-right ?: Ternary operator right-to-left x == y ? "equal" : "different" = += -= *= /= %= **= <<= >>= >>>= &= ^= |= Assignment right-to-left z = ++x % y-- yield Yield right-to-left yield x Spread not-applicable , Comma left-to-right var x = 3,y = x + 1, z = ++y An operator with higher precedence (e.g. *) is computed before the low precedence operator (e.g. +).
var x = 1, y = 2, z=3; var ans = x + y * z; console.log(ans);
7There are two operators,
+
&*
. Since*
has higher precedence over+
, that's y is multiplied by z (result:6) and then x is added to their result (1 + 6).Let's make a small change, add
()
operator.var x = 1, y = 2, z=3; var ans = (x + y) * z; console.log(ans);
9Since
()
has higher prcedence than*
and+
, that's x is added to y as they are inside the parenthesis()
(result:3). Then the result is multiplied by z (3 * 3).Notes
- Ternary operator
?:
is the single statement conditional operator alternate toif
else
- Operators are computed with respect to the operator precedence.