Difference between "==" and "===" operators in JavaScript with example.
In : BSc IT Subject : Web DesigningIn JavaScript, both == and === are comparison operators used to check if two values are equal, but they differ in how they compare those values.
== (Loose Equality Operator)
-
Compares values only , not the data types .
-
It performs type coercion , meaning it converts the data types of the values to match before comparing them.
Example:
console.log(5 == "5"); // true
Here, JavaScript converts the string
"5" into a number before comparing, so the result is true. === (Strict Equality Operator)
-
Compares both value and data type .
-
Does not perform type coercion , so the values must be the same type and value to return
true.
Example:
console.log(5 === "5"); // false
Even though the values look the same, their types are different (
number vs. string), so the result is false.