+ 1
Comparison operators in JavaScript...
What is the difference between == (compares if both values are equal), and... === (which compares if both valuables are identical, and of same type) ???? Does this mean that if I did "5==five" it would return true? i'm confused..
3 Answers
+ 7
No, it mainly refers to the types of the compared values.
For example, execute the following:
console.log('5' == 5);
console.log('5' === 5);
The first result will be True, although '5' is a string and 5 is a number. Both values are similar, but not exactly the same.
The second result will be False. The evaluation is much stricter, and ensures that both values are equal and have the same type.
If you are not sure what type a variable is, you can always use the typeof() function to verify:
console.log (typeof ('5')); // string
console.log (typeof (5)); // number
+ 2
Thank's guys!
0
Always use === to compare values, using == you might end up with unexpected results