+ 3
What is the difference between == and === in javascript?
3 Réponses
+ 12
The difference is that normal equality "==" compares the values & automatically converts the types to match the comparison if it passes the comparison then it returns true else it returns false.
Strict equality does not perform auto type conversion in case of different types for the values you compare; so only if the values you are comparing plus their type are equal it returns true else it returns false.
for example see below
console.log(2 == '2') // returns true implicit type conversion
console.log(2 === '2') // returns false no implicit type conversion
Read the mdn docs for info
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators
Check the search results for it:
https://lite.qwant.com/?q=equality+Javascript+&t=web
+ 3
== is like === but will also try to convert the two values you are comparing to the same type, sometimes in weird ways.
{ } == 0 // false
[ ] == 0 // true, what?
"10" == 10 // true
"010" == 010 // false, what?
Using === there will always return false for those examples, since the thing on the left and the thing on the right have different types.
There's really almost no reason to ever use ==. Just use === and everything will work out :)
+ 2
Thank you team💪💪