Difference between != and !== operator in javascript

Learn what is the difference between != and !== operator in javascript.

As javascript is a loosely typed programming language it can convert a value from one data type to another while performing certain actions.

Thus to check inequality of two different values there are two different operators which can be used according to our need.

The major difference between != and !== is about type conversion.

!= or loose inequality operator in javascript.

!= operator is used to check inequality of two different values. It tests the abstract inequality. Which means it does the necessary type conversion before checking the inequality.

This operator checks the inequality only after converting both the values to a common type i.e type coercion.

Input:
// ‘!=’ operator
console.log(21 != 21);
console.log(21 != '21');
console.log('food is love' != 'food is love');
console.log(true != 1);
console.log(false != 0);
console.log(false != 1);
console.log(null != undefined);

Output:
false
false
false
false
false
true
false

!== or strict inequality operator in javascript.

!== operator is used to check strict inequality of two different values. It does not do the type conversion before checking the inequality.

This operator checks inequality only after without doing the type coercion.

Input:
// ‘==’ operator
console.log(21 !== 21);
console.log(21 !== '21');
console.log('food is love' !== 'food is love');
console.log(true !== 1);
console.log(false !== 0);
console.log(false !== 1);
console.log(null !== undefined);

Output:
false
true
false
true
true
true
true

There are two types of values in the javascript true and false.

List of true values in javascript

'0'
'false' // false wrapped in string.
[]
{}
function(){}

List of false values in javascript

'' or "" // empty string
false
0
null
undefined
NaN // not-a-number

On type conversion during loose inequality checking the boolean values and other values are converted like this and thus inequality is checked. But in 90% you should avoid loose inequality checking.