JavaScript Operators

    JavaScript Operators

    JavaScript operators perform operations on operands. Operators are used to do logical and arithmetic operations. Operators that require one operand are called unary operator and operator with two operand is known as binary operator.

    For example:

    1. var sum= 55+80;

    In this example the plus (+) is the arithmetic operator and equal to (=) is the assignment operator.

    Operators in JavaScript:

    1. Arithmetic Operators
    2. Assignment Operators
    3. Bitwise Operators
    4. Special Operators
    5. Logical Operators
    6. Comparison Operators

    JavaScript Arithmetic Operators

    Arithmetic operators include addition , subtraction, multiplication operations ,Arithmetic operators are mostly used for numeric operands.
    These operators are mainly used in mathematical expressions in the same way as they are used in algebra. check the following Example of  Arithmetic Operators.

    Var num1=15, num2=5;

    Operator Description Example Result
    + Addition num1+num2 20
    Subtraction num1-num2 10
    * Multiplication num1*num2 60
    / Division num1/num2 3
    % Modulus (Remainder) num1%num2 0

    JavaScript Comparison Operators

    To do comparison between two operands Javascript comparison operator is used . 

    Following are the examples of the comparison operators:

    Var num1=5, num2=15;

    Operator Description Example Result
    == Is equal to num1==num2 false
    != Not equal to num1!=num2 true
    !== Not Identical num1!==num2 false
    > Greater than num1>num2 true
    >= Greater than or equal to num1>=num2 true
    < Less than num1<num2 false
    <= Less than or equal to num1<=num2 false

    JavaScript Bitwise Operators

    The bitwise operator works on two operands. The bitwise operators are as follows:

    Var num1 = 5, num2 = 10 , num3 = 40;

    Operator Description Example Result
    & Bitwise AND (num1==num2 & num2==num3) false
    | Bitwise OR (num1==num2 | num2==num3) false
    ^ Bitwise XOR (num1==num2 ^ num2==num3) false
    ~ Bitwise NOT (~num1) -5

    JavaScript Logical Operators

    Logical operators are used to verify more than one condition at a time or to negate the condition .it has three logical operators.

    Var num1 = 5, num2 = 10 , num3 = 40;

    Operator Description Example Example
    && Logical AND (num1==num2 && num1==num2) false
    || Logical OR (num1==num2 || num2==num3) false
    ! Logical Not !(num1==num2) true

    JavaScript Assignment Operators

    It is very important to know that assignment operator is not ‘equal to ‘ operator .The assignment operator is used to assign a value of an expression to a variable.

    Check the following JavaScript assignment operators.

    Operator Description Example with results
    = Assign 20+40 = 60
    += Add and assign var num1=20; num1+=20; Now num1 = 40
    -= Subtract and assign var num1=40; num1-=20; Now num1 = 10
    *= Multiply and assign var num1=10; num1*=40; Now num1 = 400
    /= Divide and assign var num1=20; a/=5; Now num1 = 4
    %= Modulus and assign var num1=10; num1%=2; Now num1 = 0

    JavaScript Special Operators

    The following are the examples of JavaScript special operators.

    Operator Description
    delete It is used for deleting a property from the giving object.
    instanceof Using this operator checks instanceof type giving object.
    new This operator is used to create an instance object.
    typeof This operator is used to check the type of object.
    void This operator discards the return value of expression.
    yield This is return the value of generator’s iterator 

    JavaScript Variable