JavaScript Not-Equal Operators: !== vs !=

JavaScript not-equal comparison of numbers, strings, null, and NaN

Use !== for most not-equal operators in JavaScript. It performs a strict comparison, so values must have the same type and value to be considered equal. Use != only when you deliberately want JavaScript to convert values before comparing them.

These operators can produce different results for the same pair. Choosing the right one prevents unexpected matches between numbers, strings, booleans, and nullish values.

JavaScript Not-Equal Operators: Choose !== or !=

!== is the strict inequality operator. It is the negation of strict equality: a !== b produces the same result as !(a === b). It returns true when the operands have different types or different values.

!= is the loose inequality operator. It is the negation of loose equality: a != b produces the same result as !(a == b). Before deciding whether values differ, JavaScript may convert one operand to another type.

For predictable JavaScript not-equal comparisons, prefer !==. Reserve != for cases where that conversion is an intentional part of the condition.

How does JavaScript strict inequality with !== work?

Strict inequality checks type and value without coercion. Two numbers with different values are not equal, and a number is not strictly equal to a string containing the same digits.

  • 7 !== 4 is true because the number values differ.
  • 7 !== “7” is true because one operand is a number and the other is a string.
  • “ready” !== “ready” is false because both operands are the same string.
  • null !== undefined is true because they are different types.

Strict inequality also follows JavaScript’s strict equality rules for special numeric values. In particular, NaN !== NaN is true, while 0 !== -0 is false.

How does JavaScript not-equal comparison with != handle type coercion?

Loose inequality applies the abstract equality rules before returning the opposite result. A numeric string can be converted to a number, and boolean values can be converted to numbers. This means operands that look different in source code may compare as equal.

  • 7 != “7” is false. The string is converted to the number 7.
  • 0 != false is false. The boolean is converted to 0.
  • “” != 0 is false. The empty string is converted to 0.
  • null != undefined is false. These two values are treated as equal by loose equality.

Coercion can be useful when an input is expected to contain a numeric or boolean representation. It can also hide invalid data, so a loose condition should document or clearly reflect that intent.

Which results change for numbers, strings, null, and NaN?

Which value pairs make != and !== disagree?

The operators disagree whenever loose equality converts the operands into matching values or applies its special nullish rule.

  • 5 and “5”: 5 != “5” is false, but 5 !== “5” is true.
  • 0 and false: 0 != false is false, but 0 !== false is true.
  • null and undefined: null != undefined is false, but null !== undefined is true.

For ordinary unequal values such as 3 and 8, both operators return true. The difference appears when types or special values affect the comparison.

Why does NaN need an explicit check?

NaN means “Not-a-Number,” but it is still a number value according to typeof. It is not equal to itself, so both NaN != NaN and NaN !== NaN return true. Testing whether two values differ does not identify NaN specifically.

Use Number.isNaN(value) for an explicit check. Unlike the global isNaN() function, Number.isNaN() does not first coerce unrelated values such as strings.

How do you write clear JavaScript not-equal checks?

  • Use value !== expected when both type and value must match.
  • Use typeof value !== “string” when rejecting values of a particular type.
  • Use value !== null when only the literal null should be excluded.
  • Use value != null only when intentionally treating both null and undefined as missing.
  • Convert input explicitly when conversion is required, then compare strictly: Number(input) !== 10.

In most not-equal checks in JS, !== communicates the intended rule directly and avoids implicit conversion. Choose != only when its coercive behavior is known, tested, and desirable.