Member-only story
Comparison operators in JavaScript
#4 of the 40 Essentials of Javascript — The difference between ==
and ===
In JavaScript, comparison operators are used to compare values and evaluate conditions in expressions. There are two main comparison operators: the ==
operator and the ===
operator. While they may seem similar at first glance, these operators have important differences that can have significant implications.
By the way, I have an Instagram Reel on this article, Please click here: Insta Reel
Double operator ==
The ==
operator is used to compare the values of two variables. It performs type coercion, meaning it converts the operands to the same type before making the comparison. For example, "1" == 1
is true because the string
"1" is coerced to the number
1.
const foo = 'Polestar'
const baz = 'Polestar'
cosole.log(foo == baz) // => true
cosole.log(foo === baz) // => true
Triple operator ===
On the other hand, the ===
operator is known as the strict equality operator and it does not perform type coercion. It compares the values of the operands without trying to convert them to a common type. Therefore, "1" === 1
is false because the…