=== and == in javascript
In JavaScript, the triple equals (===
) is a strict equality operator. It is used to compare two values for equality without performing type coercion. This means that not only must the values be equal, but they must also be of the same data type.
let a = 5; let b = "5";
console.log(a == b); // true, because of type coercion console.log(a === b); // false, because the types are different
In the above example, the ==
operator would return true
because JavaScript would perform type coercion and convert the string "5" to a number before making the comparison. On the other hand, the ===
operator returns false
because it checks both value and type, and the types are different.
It's generally recommended to use ===
for equality checks in JavaScript to avoid unexpected results due to type coercion.
/
Subscribe to my newsletter
Read articles from Piyush Agrawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by