JavaScript Operators

JavaScript Operators

The assignment operator (=) assigns a value to a variable. Assign values to variables and add them together:

  •       
                        var x = 5;         // assign the value 5 to x
    var y = 2;         // assign the value 2 to y
    var z = x + y;
                        

    JavaScript Arithmetic Operators

    Arithmetic operators are used to perform arithmetic on numbers:

    Much Like Algebra

    In programming, just like in algebra, we use variables (like price1) to hold values. In programming, just like in algebra, we use variables in expressions (total = price1 + price2). From the example below, you can calculate the total to be 11.

    Operator Description
    +Addition
    -Subtraction
    *Multiplication
    **Exponentiation (ES2016)
    /Division
    %Modulus (Division Remainder)
    ++Increment
    --Decrement

    JavaScript Assignment Operators

    Assignment operators assign values to JavaScript variables.

    Operator Example Same as
    =x=yx=y
    +=x+=yx=x+y
    -=x-=yx=x-y
    *=x*=yx=x*y
    /=x/=yx=x/y
    %=x%=yx=x%y
    **=x**=yx=x**y

    JavaScript String Operators

    The + operator can also be used to add (concatenate) strings.When used on strings, the + operator is called the concatenation operator

  •       
                        var txt1 = "John";
    var txt2 = "Doe";
    var txt3 = txt1 + " " + txt2; 
                        

    Adding Strings and Numbers

    Adding two numbers, will return the sum, but adding a number and a string will return a string:

  •       
                        var x = 5 + 5;
    var y = "5" + 5;
    var z = "Hello" + 5;  
                        

    JavaScript Comparison Operators

    Operator Description
    ==equal to
    ===equal value and equal type
    !=not equal
    !==not equal value or not equal type
    >greater than
    <less than
    >=greater than or equal to
    <=less than or equal to
    ?ternary operator

    JavaScript Logical Operators

    Operator Description
    &&logical and
    ||logical or
    !=logical not

    JavaScript Type Operators

    Operator Description
    TypeofReturns the type of a variable
    InstanceofReturns true if an object is an instance of an object type

    JavaScript Type Operators

    Operator Description Example Same as Result Decimal
    &AND5 & 10101 & 000100011
    |OR5 | 10101 | 000101015
    ~NOT5 ~ 1~0101101010
    ^XOR5 ^ 10101 ^ 000101004
    <<Zero fill left shift5 << 10101 << 1101010
    >>Signed right shift5 >> 15 << 10101 >> 100102
    <<<Signed right shift5<<<15 <<< 10101 <<< 110102

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1