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 consider x = 1, y = 2 and z = 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

    OperatorExampleResult
    == 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 like else 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 unlike if 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.

    OperatorNameAssociationExample
    ()Parenthesis Groupinga*(c+d)
    .Object member accessleft-to-rightarr.length
    []Computed Member Accessleft-to-rightarr[0]
    newobject creation and constructor argumentsnot-applicablenew Array(5,23,680)
    ()Function callleft-to-rightparseInt("3")
    newobject creation with empry constructorright-to-leftnew Array()
    ++,--Postfix Increment, Postfix Decrementnot-applicablex++,y--
    ! ~ + - ++ -- typeof void deleteLogical NOT, Bitwise NOT, Unary Plus, Unary Nevation, Prefix Increment, Prefix Decrement, typeof statement, void keyword, delete statementright-to-left!++x>--y
    **Exponentiation (exponent/power)right-to-left
    %Remainder/Modulusleft-to-right
    * /Product (Multiply/Divide)left-to-right
    + -Sum (Plus/Minus)left-to-right
    << >> >>>Bitwise Shift
    < <= > >= in instanceofRelational Operators, in keyword, instanceof keywordleft-to-right
    == != === !==Equality, Inequality, Strict Equality and Strict Inequality Operatorsleft-to-right
    &Bitwise ANDleft-to-right
    ^Bitwise XORleft-to-right
    |Bitwise ORleft-to-right
    &&Logical ANDleft-to-right
    ||Logical ORleft-to-right
    ?:Ternary operatorright-to-leftx == y ? "equal" : "different"
    = += -= *= /= %= **= <<= >>= >>>= &= ^= |=Assignmentright-to-leftz = ++x % y--
    yieldYieldright-to-leftyield x
    Spreadnot-applicable
    ,Commaleft-to-rightvar 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); 7

    There 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); 9

    Since () 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

    1. Ternary operator ?: is the single statement conditional operator alternate to if else
    2. Operators are computed with respect to the operator precedence.