Write the main difference between '==' and '===' operators in javascript?

JavascriptObject Oriented ProgrammingProgramming

The difference between '==' and '===' is that former checks only value but the latter checks value and also data type(String, Boolean etc).The following example gives whether values assigned are equal or not irrespective of datatype. 

a) "==" operator(checks equality)

 Example

 Live Demo

<html>
<body>
<p id="strict"></p>
<script>
   var x = 5;
   var y = 5;
   var z = 6;
   document.getElementById("strict").innerHTML =
   (x == y) + "<br>" + (x == z);
</script>
</body>
</html>

Output

true
false


b) '===' operator (Checks strict equality) 

    "==="  operator gives true if and only if both value and data type are equal.If not it returns false.In the following            example every variable(x,y,z) has assigned value 5 but some of them were given string data type(variables y and z).When we strict equals x and y we get false because variable y is assigned string data type whereas variable x is not.  

 Example

 Live Demo

<html>
<body>
<p id="strict"></p>
<script>
   var x = 5;
   var y = "5";
   var z = "5";
   document.getElementById("strict").innerHTML =
   (x === y) + "<br>" + (x == z);
</script>
</body>
</html>

Output

false
true
raja
Published on 21-May-2019 16:07:12
Advertisements