Open In App

Difference between != and !== operator in JavaScript

Last Updated : 29 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

!= operator

The inequality operator (!=) is the logical opposite of the equality operator. It means “Not Equal” and returns true whereas 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 takes place so 1 and ‘1’ are considered equal.

Example: Below example shows != Operator.

Javascript




console.log(" 1 != '1' " + (1 != '1'));
console.log(" 1 != 1 " + (1 != 1));
console.log(" 1 != '2' " + (1 != '2'));


Output

 1 != '1' false
 1 != 1 false
 1 != '2' true

!== operator

The strict inequality operator (!==) is the logical opposite of the strict equality operator. It means “Strictly Not Equal” and returns true whereas 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 takes place.

Example: Below example shows !== Operator.

Javascript




console.log(" 1 !== '1' " + (1 !== '1'));
console.log(" 1 !== 1 " + (1 !== 1));
console.log(" 1 !== '2' " + (1 !== '2'));


Output

 1 !== '1' true
 1 !== 1 false
 1 !== '2' true

Difference between != and !== operator in JavaScript

Feature != (Loose Inequality) !== (Strict Inequality)
Type Coercion Converts values to the same type for comparison. Compares values without changing their types.
Example 5 != "5" is false 5 !== "5" is true
Usage Recommendation Use with caution as it may lead to unexpected results due to type coercion. Preferred in most cases to ensure both value and type are considered. It’s more explicit and reduces the chance of unexpected behavior.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads