Skip to content

05 Operators

Biswajit Sundara edited this page May 1, 2023 · 3 revisions

Arithmetic Operators

Java script supports the arithmetic operators -

Addition (+), Subtraction (-), Multiplication (*), Division (/), 

Modulus/Remainder (%), Increment (++), Decrement (--)

Comparison Operators

Java script supports the comparison operators

Equal to (=), Identical(===), Not equal to (!=), Not Identical (!===), 

Greater than (>), Less than (<), Greater than equal to (>=), 

Less than Equal (<=) 

Logical Operators

AND(&&), OR (||), Not (!)

Assignment Operator

The assignment operator is (=) we can combine the arithmetic operators with assign operator also E.g += will first add and then assign the value.

let a=10;
a+=20;
This will first add 20 to the value of a and then assign the whole value. 
So the value of a= 30

Ternary Operator

Using this operator we can check in line conditions. This improves the verbose and reduces the boiler plate code. Please note, if we have multiple lines of code in If or Else block then this approach can't be used. This is helpful if we have single statements within If /Else blocks.

Syntax - (expression) ? value if true: value if false
Let's say we have to check this
if(a>b)
{
    console.log('a is greater than b')
}
else
{
    console.log('b is greater than a');
}
The same thing can be easily done using ternary operator

(a>b) ? console.log('a is greater than b') : console.log('b is greater than a');

Difference between == and ===

== means equal and returns true if values match however === means identical and will return true only if values & data types both match.

function equal()
{
   let a=10;
   let b="10";

   if(a==b)
   {
       console.log('equals');
   }
}

function identical()
{
   let a=10;
   let b="10";

   if(a===b)
   {
       console.log('identical');
   }
   else
   {
    console.log('not identical');
   }
}
equal();
identical();

So you will see it will print equals and not identical because even though the values are same, one is number and another one is string data type.

Clone this wiki locally