Comparison with the Strict Inequality Operator in JavaScript

Posted by

In JavaScript, the strict inequality operator (!==) compares two values for inequality and returns true if the values are not equal, and false if they are equal. This operator compares both the value and the type of the operands.

For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

console.log(5 !== 5); // false
console.log(5 !== '5'); // true
console.log(5 !== 6); // true

[/dm_code_snippet]

It is important to note that the strict inequality operator should be used instead of the regular inequality operator (!=) in JavaScript, as the latter performs type coercion, meaning it converts the operands to the same type before making the comparison.

For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

console.log(5 != 5); // false
console.log(5 != '5'); // false
console.log(5 != 6); // true

[/dm_code_snippet]

When working with the strict inequality operator in JavaScript, it’s important to understand that it will only return true if both the value and the type of the operands are different. This means that if you compare two variables, one being a number and the other being a string that contains the number in its string representation, the strict inequality operator will return true.

For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let num = 5;
let str = '5';
console.log(num !== str); // true

[/dm_code_snippet]

On the other hand, if you want to compare only the value of the operands and ignore their types, you can use the loose equality operator (==) or the Object.is() method.

For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let num = 5;
let str = '5';
console.log(num == str); // true
console.log(Object.is(num, str)); // true

[/dm_code_snippet]

It’s also important to note that when working with null and undefined values, the strict inequality operator will return true when comparing them to any other value, including other null and undefined values.

For example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let x = null;
let y = undefined;
console.log(x !== y); // true

[/dm_code_snippet]

In general, it’s recommended to use the strict inequality operator when comparing values in JavaScript, as it provides a more accurate and predictable comparison. However, in some cases, you may want to use the loose equality operator or the Object.is() method to compare values regardless of their types.