References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Inequality != operator

Operator JavaScript All modern browsers Updated
Quick answer

The inequality operator != returns true when two values are not equal, after coercing their types — it's the negation of ==. Because it coerces, 0 != "" is false. Prefer strict inequality (!==), which doesn't coerce.

Overview

!= is simply the opposite of loose equality (==): it returns true when the two values are not equal. And it inherits exactly the same behavior — it coerces types before comparing. So 1 != "1" is false (they're "equal" after coercion), and 0 != "" is false too.

That carries the same confusion as ==, which is why the advice is identical: use strict inequality (!==) instead. It checks "not equal" without any type juggling, so 1 !== "1" is correctly true.

The only place you'll reasonably see != is the mirror of the == null idiom: x != null means "x is neither null nor undefined" — a concise "has a value" check. Outside that, default to !==.

Syntax

a != b

1 != "1"      // false (coerced equal)
0 != ""       // false
1 !== "1"     // true  (strict, preferred)

x != null     // "x has a value" (not null and not undefined)

Example

Live example
<pre id="out" style="font:14px ui-monospace,monospace"></pre>
<script>
  document.getElementById('out').textContent =
    '1 != "1"  -> ' + (1 != '1') + '\n' +
    '1 !== "1" -> ' + (1 !== '1') + '\n' +
    '5 != 5    -> ' + (5 != 5);
  // false / true / false
</script>

Best practices

  • Use !== by default — it avoids the coercion of !=.
  • The one idiomatic use of != is x != null (not null and not undefined).
  • Pair your equality choices: use ===/!== together, not a mix.
  • Let your linter flag !=, the same as ==.

Frequently asked questions

What is the difference between != and !== ?
!= coerces types before comparing; !== does not. Use !==.
Why is 0 != "" false?
Because != coerces, and 0 loosely equals "", so "not equal" is false. With !== it's true.
What does x != null mean?
It's true when x is neither null nor undefined — a concise "x has a value" check.
Should I ever use !=?
Generally no — prefer !==. The accepted exception is the != null idiom.