Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Write the main difference between '==' and '===' operators in javascript?
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
<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
<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
Advertisements
