JavaScript Operators

  


JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic on numbers:

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

   
    <script>

        let a = 100;
        let b = 50;
        let x = a + b;

        // let x = (100 + 50) * a;

        document.write(x);

    </script>

Comparison Operators

Comparison and Logical operators are used to test for true or false.

Comparison operators are used in logical statements to determine equality or difference between variables or values.

 
    <script>

        let x = 5;

        document.write(x == 8);

    </script>

Given that x = 5, the table below explains the comparison operators:

OperatorDescriptionComparingReturns
==equal tox == 8false
x == 5true
x == "5"true
===equal value and equal typex === 5true
x === "5"false
!=not equalx != 8true
!==not equal value or not equal typex !== 5false
x !== "5"true
x !== 8true
>greater thanx > 8false
<less thanx < 8true
>=greater than or equal tox >= 8false
<=less than or equal tox <= 8



true

JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

OperatorExampleSame 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

The addition assignment operator (+=) adds a value to a variable.

   
    <script>

        let x = 5;
        let y = 2;
        let z = x + y;

        document.write(z);

    </script>
   


Logical Operators

Logical operators are used to determine the logic between variables or values.Given that x = 6 and y = 3, the table below explains the logical operators:

        <script>

        let x = 5;

        document.write((x == 5 || y == 5));

    </script>


OperatorDescriptionExample
&&and(x < 10 && y > 1) is true
||or(x == 5 || y == 5) is false
!not!(x == y) is true

JavaScript Operator Precedence Values

ValueOperatorDescriptionExample
21( )Expression grouping(3 + 4)
    
20.Memberperson.name
20[]Memberperson["name"]
20()Function callmyFunction()
20newCreatenew Date()
    
18++Postfix Incrementi++
18--Postfix Decrementi--
    
17++Prefix Increment++i
17--Prefix Decrement--i
17!Logical not!(x==y)
17typeofTypetypeof x
    
16**Exponentiation (ES2016)10 ** 2
    
15*Multiplication10 * 5
15/Division10 / 5
15%Division Remainder10 % 5
    
14+Addition10 + 5
14-Subtraction10 - 5
    
13<<Shift leftx << 2
13>>Shift rightx >> 2
13>>>Shift right (unsigned)x >>> 2
    
12<Less thanx < y 
12<=Less than or equalx <= y
12>Greater thanx > y
12>=Greater than or equalx >= y
12inProperty in Object"PI" in Math
12instanceofInstance of Objectinstanceof Array
    
11==Equalx == y
11===Strict equalx === y
11!=Unequalx != y
11!==Strict unequalx !== y
    
10&Bitwise ANDx & y
9^Bitwise XORx ^ y
8|Bitwise ORx | y
7&&Logical ANDx && y
6||Logical ORx || y
5??Nullish Coalescingx ?? y
4? :Condition? "Yes" : "No"
    
3+=Assignmentx += y
3/=Assignmentx /= y
3-=Assignmentx -= y
3*=Assignmentx *= y
3%=Assignmentx %= y
3<<=Assignmentx <<= y
3>>=Assignmentx >>= y
3>>>=Assignmentx >>>= y
3&=Assignmentx &= y
3^=Assignmentx ^= y
3|=Assignmentx |= y
    
2yieldPause Functionyield x
1,Comma5 , 6

Expressions in parentheses are fully computed before the value is used in the rest of the expression.