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
// x has not been declared before // evaluates to true without errors if (typeof x === "undefined") { // these statements execute } // Throws a ReferenceError if (x === undefined) { } However, there is another alternative. JavaScript is a statically scoped language, so knowing if a variable is declared can be read by seeing whether it is declared in an enclosing context.
🌐
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' ?
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)
🌐
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, ...
🌐
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....
Find elsewhere
🌐
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 ...
🌐
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-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/
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › falsy-in-javascript
Falsy in JavaScript - GeeksforGeeks
July 23, 2025 - Although both are falsy, they are used differently. null often represents a absence of a value, while undefined usually indicates it is not yet initialize.
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

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Boolean
Boolean - JavaScript | MDN
However, when comparing with false, ... == false is true. In general, falsiness and == false differ in the following cases: NaN, undefined, and null are falsy but not loosely equal to false....
🌐
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.
🌐
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.
🌐
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.
🌐
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 12, 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 ...
🌐
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...