Difference Between == and === in Javascript !

== and === are used to check compare operator .
== variable is checked for value equality only, before checking the value it converts the value into another type to match each other
=== variable is checked for same value as well as same type .
let's talk about an example for a better understanding of both operands
Example 1:
const demo = "javascript"
const test = "vanilaJs"
console.log(demo == test) //true
console.log(demo === test) //true
Reason: so here as we see in both operands return true because type and value of variable are same .
Example 2:
const num1 = 1234
const numString = '1234'
console.log(num1 == numString) //true
console.log(num1 === numString) //false
Reason:In this case (num1 == num2 ) returns true because it is converted to a number before comparing and (num1 === numString ) returns false value is same but type is string.
Example 3:
console.log(0 == false) //true
console.log(0 === false) //false
Reason: same value, different type In this case. The value of 0, when checked with false, is same. It is so because 0 and false have the same value for JavaScript, but when checked for type and value, the value is false because 0 is a number and false is a boolean.
Example 4:
const str = " "
console.log(str == false) //true
console.log(str === false) //false
Reason:Same here. the value of empty string and false in same in javascript so it returns true in (==) but (===) type is boolean so it returns false.
That's it .Hope Js developer understand these fundamental of javascript
About the author
Do More than Just Exist!!
Comments
Join the discussionNo comments yet. Be the first to comment.


