Difference between != and !== operator in JavaScript
- != operator
- The inequality operator (!=) is the logical opposite of the equality operator. It means “Not Equal” and returns true where equality would return false and vice versa.
- Like the equality operator, the inequality operator will convert data types of values while comparing.
- For example 1 != ‘1’ will return false since data type conversion take place so 1 and ‘1’ are considered equal.
- !== operator
- The strict inequality operator (!==) is the logical opposite of the strict equality operator. It means “Strictly Not Equal” and returns true where strict equality would return false and vice versa.
- Strict inequality will not convert data types.
- For example 1 !== ‘1’ will return true since 1 is an integer and ‘1’ is a character and no data type conversion take place.
Example:
<script> function NotEqual(val) { if (val != 1) { return "Both are Not Equal i.e. condition is true"; } else { return "Both are Equal i.e. condition is false"; } } function StrictlyNotEqual(val) { if (val !== 1) { return "Both are Not Equal i.e. condition is true"; } else { return "Both are Equal i.e. condition is false"; }} //Passing integer console.log(" 1 != 1 " + NotEqual(1));console.log(" 1 !== 1 " + StrictlyNotEqual(1)); //Passing character console.log(" '1' != 1 " + NotEqual('1'));console.log(" '1' !== 1 " + StrictlyNotEqual('1')); //Passing string console.log(" \"1\" != 1 " + NotEqual("1"));console.log(" \"1\" !== 1 " + StrictlyNotEqual("1")); //Passing false value i.e. 0 console.log(" 0 != 1 " + NotEqual(0));console.log(" 0 !== 1 " + StrictlyNotEqual(0)); </script> |
Output:



