What does “!” operator mean in javascript when it is used with a non-boolean variable?

Any falsy value will satisfy the if(!insert_variable_here) condition, including:

  • false
  • null
  • undefined
  • The empty string ''
  • The number 0
  • NaN

If callback return evaluates any of those values, the condition will be satisfied.

Even though null != false, the following will give you an alert:

x = null;
if(!x) {
    alert('"!null" does evaluate to true');
}

Regardless of whether or not null != false makes sense to you or anyone else, the point is that in JavaScript null is a falsy value, and thus a value that would satisfy the condition in my first bit of code listed above. This, it seems, is the question you have asked–not, rather, if null should or should not == false.

Leave a Comment