In JavaScript, ==
and ===
are comparison operators used to check for equality between two values.
The main difference between ==
and ===
is that double equal operator performs loose or abstract comparison while triple
equal performs strict comparison.
The double equal operator first try to coerce the value and then do the comparison while the triple equal operator do the comparison only.
—
The ==
operator compares values after performing type coercion, which means it tries to convert both operands to a common type before comparing them.
For example:
1 == "1"; // true
In the above example, the string ‘1’ is converted to the number 1 before the comparison is made. So the statement returns true.
On the other hand, the ===
operator compares values without performing any type coercion. It only returns true if the values being compared have the same type and the same value.
For example:
1 === "1"; // false
In the above example, the comparison returns false because the values being compared are of different types (number and string).
Generally it is recommended to use the ===
operator to avoid any type coercion.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.