I have noticed quite a few people use three equals signs when comparing things in Javascript, but I was taught to only use two. Can anyone shed some light on why someone would use three or two, and why do they both work?
-Thanks :)
Another user has pointed out that the question has already been asked, sorry about that guys, going to look at the answers on that one.
All the following evaluations will return true
With ==
JS will type-juggle.
1 == '1'
1 == 1
1 == true
0 != true
0 == false
With ===
JS will not type-juggle
1 !== '1'
1 === 1
1 !== true
0 !== false
The identity === operator is identical to the equality == operator except no type conversion is done therefore the types must be the same to be considered equal.
The == operator will compare for equality after doing any necessary type conversions.
The === operator will not do the conversion, so if two values are not the same type === will simply return false.
For me I generally always use ===
or !==
, so as not to leave anything to chance.
It depends on the use case. Triple equals is to check for identicality; in other words, not only equivalent, but the same type. Here's a good reference
Triple equal will check the type of the variable also while double equal will only check for the match. If you want yo check the type of the variable, you need to use triple equals. Else you just need you use double equals.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With