JavaScript Arithmetic

JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic on numbers:

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

Arithmetic Operations

A typical arithmetic operation operates on two numbers. The two numbers can be literals:

  •       
                        var x = (100 + 50) * a; 
                          
                        

    The numbers (in an arithmetic operation) are called operands. The operation (to be performed between the two operands) is defined by an operator.

    Operand Operator Operand
    100+ 50

    Adding, subtract, multiply and divide operators

    The addition operator (+) adds numbers:
    The subtraction operator (-) subtracts numbers.
    The multiplication operator (*) multiplies numbers.
    The division operator (/) divides numbers.

  •       
                        var x = 5;
    var y = 2;
    var z = x + y; 
    var z = x - y;   
    var z = x * y;
    var z = x / y;
                        

    Remainder

    The modulus operator (%) returns the division remainder. >

    In arithmetic, the division of two integers produces a quotient and a remainder. In mathematics, the result of a modulo operation is the remainder of an arithmetic division.

    Incrementing

    The increment operator (++) increments numbers.

  •       
                        var x = 5;
    x++;
    var z = x;
                        

    Decrementing

    The decrement operator (--) decrements numbers.

  •       
                        var x = 5;
    x--;
    var z = x; 
                        

    Exponentiation

    The exponentiation operator (**) raises the first operand to the power of the second opera

    x ** y produces the same result as Math.pow(x,y):

  •       
                        var x = 5;
    var z = x ** 2;  
    var z = Math.pow(x,2);   // result is 25                            
                     < 
                        

    Operator Precedence

    Operator precedence describes the order in which operations are performed in an arithmetic expression.

  •       
                        var x = 100 + 50 * 3; 
                        

    As in traditional school mathematics, the multiplication is done first.

    Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). And (as in school mathematics) the precedence can be changed by using parentheses:

  •       
                        var x = (100 + 50) * 3;   
                        

    When using parentheses, the operations inside the parentheses are computed first.

    When many operations have the same precedence (like addition and subtraction), they are computed from left to right:

    JavaScript Operator Precedence Values

    Pale red entries indicates ECMAScript 2015 (ES6) or higher.

    MemberLess than or equal
    Value Operator Description Example
    20( )expressions Grouping (3+4)
    19. person.name
    19[]Member person["name"]
    19()Function call myFunction()
    19newcreate new Date()
    17++Postfix Increment i++
    17--Postfix Decrement i--
    16++Prefix Increment ++i
    16--Prefix Decrement --i
    16!Logical Not !(x==y)
    16typeof xtype typeof x
    15**Exponentiation (ES2016) 10**2
    14*Multiplication 10*2
    14/Division 10/2
    14%Remainder 10%2
    13+Addition 10+2
    13-Substraction 10-2
    12<<Shift left x<<2
    12>>Shift right x>>2
    12>>>Shift right (unsigned)) x>>>2
    11<Less than x<y
    11<= x<=y
    11>greater than x>y
    11>=Greater than or equal x>y
    11inProperty of object "PI" in Math
    11instanceofInstance of object instanceof Array
    10==Equal x=y
    10===Strictly equal x===y
    10!=Unqual x!=y
    10!==Strictly unequal x!==y
    9&Bitwise AND x&y
    8^Bitwise XOR x ^ y
    7|Bitwise OR x|y
    6&&Logical AND x&&y
    5||Logical OR x||y
    4?:Condition ? "Yes" : "No"
    3+=Assignment x+=y
    3/=Assignment x/=y
    3-=Assignment x-=y
    3*=Assignment x*=y
    3%=Assignment x%=y
    3<<=Assignment x<<=y
    3>>=Assignment x>>=y
    3>>>=Assignment x>>>=y
    3%=Assignment x&=y
    3^=Assignment x^=y
    3|=Assignment x|=y
    3yeildPause Function x yeild y
    3, comma 5,6

    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