web developing
21 Mar
I know this is really for beginners, but have you ever asked yourself what are the differences between == and === when comparing in JavaScript, and when to use it? Here’s an example:
var a = false; alert(a === 0); // prints false
That’s false. Even when both the zero and the variable are faulty the entire operation is false. Because the 0 is a number and the variable a is boolean, therefore their different types give us the answer. If there was a comparision only with == the result would be true:
var a = false; alert(a == 0); // prints true
It’s good when you expect some variable to match certain type to compare with ===. So the correct code is:
var a = false; alert(a === false); // prints true
Here the type of a compares with boolean. See the complete demo.
Related posts:
2 Responses for "JavaScript Comparision Snippet"
good catch
good to know
Leave a reply