Play Games

Search This Blog

Monday, November 13, 2017

Difference between == and ===

'==' checks whether both values are equal or not where as '===' checks both value and type are equal or not.
Example 1:
<html>
<Script>
window.onload = function check() {
var a = 5;
var b = '5';
if(a=== b) {
alert("a and b are equal.Triple Equal checks both value and type are equal or not");
}
if(a==b) {
alert("a and b are equal.Double Equal check only whether value is same but not its type");
}
}
</script>

</html>
if we run the above code,it displays the alert "a and b are equal.Double Equal check only whether value is same but not its type" because a and b have same value 5 but type of a is number and type of b is string.So,it wont display other alert related to '==='.

Example 2:

<html>
<Script>
window.onload = function check() {
var a = 5;
var b = 5;
if(a=== b) {
alert("a and b are equal.Triple Equal checks both value and type are equal or not");
}
if(a==b) {
alert("a and b are equal.Double Equal check only whether value is same but not its type");
}
}
</script>

</html>

It displays both the alerts as both value and type of a and b are equal.

No comments:

Post a Comment