Comparison operators in JavaScript... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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..

18th Jun 2018, 1:59 AM
Kyle
Kyle - avatar
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
18th Jun 2018, 2:14 AM
Mickel
Mickel - avatar
+ 2
Thank's guys!
18th Jun 2018, 2:19 AM
Kyle
Kyle - avatar
0
Always use === to compare values, using == you might end up with unexpected results
18th Jun 2018, 6:37 AM
damyco
damyco - avatar