It means that undefined is a falsy value, list of falsy values are:

""        // Empty string
null      // null
undefined // undefined, which you get when doing: var a;
false     // Boolean false
0         // Number 0
NaN       // Not A Number eg: "a" * 2

If you negate a falsy value you will get true:

!""        === true
!null      === true
!undefined === true
!0         === true
!NaN       === true

And when you nagate a truthy value you will get false:

!"hello" === false
!1       === false

But undefined is not equal false:

undefined === false // false
undefined  == false // false

And just for the fun if it:

undefined == null // true
Answer from Andreas Louv on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript | MDN
You can use undefined and the strict equality and inequality operators to determine whether a variable has a value. In the following code, the variable x is not initialized, and the if statement evaluates to true.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Falsy
Falsy - Glossary | MDN
A falsy (sometimes written falsey) ... conditionals and loops. The following table provides a complete list of JavaScript falsy values: The values null and undefined are also nullish....
🌐
Webmaster World
webmasterworld.com › javascript › 5090854.htm
Easy way to convert undefined to false? - JavaScript and AJAX forum at WebmasterWorld - WebmasterWorld
var[counter] ?= false; My goal here is to minimize the code to shave off a few microseconds :-) ... My intuition says if you don't know the inputs then it's undefined behaviour. That said, typeof x === 'undefined' So (y = typeof(x === 'undefined' ?
🌐
freeCodeCamp
freecodecamp.org › news › javascript-check-if-undefined-how-to-test-for-undefined-in-js
JavaScript Check if Undefined – How to Test for Undefined in JS
November 7, 2024 - An undefined variable or anything without a value will always return "undefined" in JavaScript. This is not the same as null, despite the fact that both imply an empty state. You'll typically assign a value to a variable after you declare it, ...
Top answer
1 of 5
24

In programming, truthiness or falsiness is that quality of those boolean expressions which don't resolve to an actual boolean value, but which nevertheless get interpreted as a boolean result.

In the case of C, any expression that evaluates to zero is interpreted to be false. In Javascript, the expression value in

if(value) {
}

will evaluate to true if value is not:

null
undefined
NaN
empty string ("")
0
false

See Also
Is there a standard function to check for null, undefined, or blank variables in JavaScript?

2 of 5
9

The set of "truthy" and "falsey" values in JavaScript comes from the ToBoolean abstract operation defined in the ECMAScript spec, which is used when coercing a value to a boolean:

+--------------------------------------------------------------------------+
| Argument Type | Result                                                   |
|---------------+----------------------------------------------------------|
| Undefined     | false                                                    |
|---------------+----------------------------------------------------------|
| Null          | false                                                    |
|---------------+----------------------------------------------------------|
| Boolean       | The result equals the input argument (no conversion).    |
|---------------+----------------------------------------------------------|
| Number        | The result is false if the argument is +0, −0, or NaN;   |
|               | otherwise the result is true.                            |
|---------------+----------------------------------------------------------|
| String        | The result is false if the argument is the empty String  |
|               | (its length is zero); otherwise the result is true.      |
|---------------+----------------------------------------------------------|
| Object        | true                                                     |
+--------------------------------------------------------------------------+

From this table, we can see that null and undefined are both coerced to false in a boolean context. However, your fields.length === 0 does not map generally onto a false value. If fields.length is a string, then it will be treated as false (because a zero-length string is false), but if it is an object (including an array) it will coerce to true.

If fields should be a string, then !fields is a sufficient predicate. If fields is an array, your best check might be:

if (!fields || fields.length === 0)
Top answer
1 of 8
47

With the original answer pointing to the spec being deleted, I'd like to provide a link and short excerpt from the spec here.

http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

The ECMA spec doc lists the reason why undefined == false returns false. Although it does not directly say why this is so, the most important part in answering this question lies in this sentence:

The comparison x == y, where x and y are values, produces true or false.

If we look up the definition for null, we find something like this:

NULL or nil means "no value" or "not applicable".

In Javascript, undefined is treated the same way. It is without any value. However, false does have a value. It is telling us that something is not so, whereas undefined and null are not supposed to be giving any value to us. Likewise, there is nothing it can convert to for its abstract equality comparison therefore the result would always be false. It is also why null == undefined returns true (They are both without any value). It should be noted though that null === undefined returns false because of their different types. (Use typeof(null) and typeof(undefined) in a console to check it out)

What I'm curious of though, is that comparing NaN with anything at all will always return false. Even when comparing it to itself. [NaN == NaN returns false]

Also, another odd piece of information: [typeof NaN returns "number"]


Strict Equality

If possible, you should avoid using the == (two equals) operator to compare two values. Instead use === (three equals) to truly see if two values are equal to each other. == gives the illusion that two values really are exactly equal when they may not be equal by using coercion. Examples:

5 == "5" is true

5 === "5" is false

"" == false is true

"" === false is false

0 == false is true

0 === false is false

2 of 8
1

From the incomparable MDN, sponsored by the company of JavaScript's creator.

JavaScript provides three different value-comparison operations:

  • strict equality (or "triple equals" or "identity") using ===,
  • loose equality ("double equals") using ==,
  • and Object.is (new in ECMAScript > 6).

The choice of which operation to use depends on what sort of comparison you are looking to perform.

Briefly, double equals will perform a type conversion when comparing two things; triple equals will do the same comparison without type conversion (by simply always returning false if the types differ); and Object.is will behave the same way as triple equals, but with special handling for NaN and -0 and +0 so that the last two are not said to be the same, while Object.is(NaN, NaN) will be true. (Comparing NaN with NaN ordinarily—i.e., using either double equals or triple equals—evaluates to false, because IEEE 754 says so.) Do note that the distinction between these all have to do with their handling of primitives; none of them compares whether the parameters are conceptually similar in structure. For any non-primitive objects x and y which have the same structure but are distinct objects themselves, all of the above forms will evaluate to false.

For a visual overview of the whole picture of equality in JavaScript: https://dorey.github.io/JavaScript-Equality-Table/

The truth is, this seemingly "bad" aspect of JavaScript is a source of power when you understand how it works.

Find elsewhere
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
In the strictest sense, null represents ... operator coerces operands of different types to boolean values, making null and undefined both false....
🌐
freeCodeCamp
freecodecamp.org › news › falsy-values-in-javascript
Falsy Values in JavaScript
December 14, 2019 - Description A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsey values in JavaScript: undefined, null, NaN, 0, "" (empty string), and false of course. Checking for falsy values ...
🌐
CodeBurst
codeburst.io › understanding-null-undefined-and-nan-b603cb74b44c
Understanding null, undefined and NaN. | by Kuba Michalski | codeburst
November 12, 2018 - Negating null value returns true , but comparing it to false (or either true) gives false. In basic maths operations, null value is converted to 0. The global undefined property represents the primitive value undefined.
🌐
Greenroots
blog.greenroots.info › javascript-undefined-and-null-lets-talk-about-it-one-last-time
JavaScript undefined and null: Let's talk about it one last time!
November 6, 2020 - undefined and null are primitive values and they represent falsy values. All the similarities between undefined and null end here. undefined value is typically set by the JavaScript engine when a variable is declared but not assigned with any ...
🌐
Quora
quora.com › What-is-the-difference-between-undefined-null-and-false
What is the difference between 'undefined', 'null', and 'false'? - Quora
Answer: In most programming languages there is undefined, null, and false is a value given to a Boolean variable. When it comes to any variable null means that it wasn’t assigned any value.
🌐
Quora
quora.com › Why-is-null-undefined-true-in-JavaScript
Why is (null==undefined) true in JavaScript? - Quora
Answer (1 of 7): Actually, there is no satisfactory reason for that. I am too confused with this. Some say both are actually falsy values, so it evaluates to true, but i don’t get this logic. I mean i want to know what thing is being converted to what if type coercion is happening.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-determine-if-variable-is-undefined-or-null-in-javascript.php
How to Determine If Variable is Undefined or NULL in JavaScript
<script> var firstName; var lastName ... // Print: true console.log(null === undefined) // Print: false /* Since null == undefined is true, the following statements will catch both null and undefined */ if(first...
🌐
Quora
quora.com › Why-does-JavaScript-return-true-when-a-variable-is-not-defined
Why does JavaScript return true when a variable is not defined? - Quora
Answer (1 of 2): It doesn’t, or at least shouldn’t, since undefined is one of the falsy values. https://www.30secondsofcode.org/js/s/truthy-falsy-values/
🌐
Bennadel
bennadel.com › blog › 4783-comparing-undefined-values-with-optional-chaining-in-javascript.htm
Comparing Undefined Values With Optional Chaining In JavaScript
April 17, 2025 - But, as we've demonstrated above, the undefined value always results in false when compared to numbers. Which means this: ... This JavaScript behavior can simplify our if blocks when it comes to things lie the indexOf() comparison.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › falsy-in-javascript
Falsy in JavaScript - GeeksforGeeks
July 23, 2025 - false is falsy. 0 is falsy. 0 is falsy. 0 is falsy. is falsy. null is falsy. undefined is falsy. NaN is falsy. Understanding falsy values can help avoid common pitfalls in JavaScript
Top answer
1 of 4
44

Is this the expected behavior.

Yes.

If so then why ?Am I missing some concept/theory about undefined in Javascript?

JavaScript has the concept of implicit conversion of values (aka coercing values). When you use the negation ("NOT") operator (!), the thing you're negating has to be a boolean, so it converts its argument to boolean if it's not boolean already. The rules for doing that are defined by the specification: Basically, if the value is undefined, null, "", 0, 0n, or NaN (also document.all on browsers¹), it coerces to false; otherwise, it coerces to true.

So !undefined is true because undefined implicitly converts to false, and then ! negates it.

Collectively, those values (and false) are called falsy values. Anything else¹ is called a truthy value. This concept comes into play a lot, not just with !, but with tests in ifs and loops and the handling of the return value of callbacks for certain built-in functions like Array.prototype.filter, etc.


¹ document.all on browsers is falsy, even though it's an object, and all (other) objects are truthy. If you're interested in the...interesting...history around that, check out Chapter 17 of my recent book JavaScript: The New Toys. Basically, it's to avoid sites unnecessarily using non-standard, out of date features.

2 of 4
3

Yes, it is the expected behavior.

Negation of the following values gives true in javaScript:

  • false
  • undefined
  • null
  • 0 (number zero)
  • ""(empty string)

eg: !undefined = true

Note: The following checks return true when you == compare it with false, but their negations will return false.

  • " "(space only).
  • ,

eg: [ ] == false gives true, but ![ ] gives false