undefined means a variable has been declared but has not yet been assigned a value :

var testVar;
console.log(testVar); //shows undefined
console.log(typeof testVar); //shows undefined

null is an assignment value. It can be assigned to a variable as a representation of no value :

var testVar = null;
console.log(testVar); //shows null
console.log(typeof testVar); //shows object

From the preceding examples, it is clear that undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.

Proof :

console.log(null === undefined) // false (not the same type)
console.log(null == undefined) // true (but the "same value")
console.log(null === null) // true (both type and value are the same)

and

null = 'value' // Uncaught SyntaxError: invalid assignment left-hand side
undefined = 'value' // 'value'
Answer from sebastian on Stack Overflow
🌐
Reddit
reddit.com › r/typescript › undefined vs null
r/typescript on Reddit: Undefined vs null
February 27, 2023 -

Since switching to TypeScript I have been using a lot of optional properties, for example:

type store = {
  currentUserId?: string
}

function logout () {
  store.currentUserId = undefined
}

However my coworkers and I have been discussing whether null is a more appropriate type instead of undefined, like this:

type store = {
  currentUserId: string | null
}

function logout () {
  store.currentUserId = null
}

It seems like the use of undefined in TypeScript differs slightly from in Javascript.

Do you guys/girls use undefined or null more often? And, which of the examples above do you think is better?

Discussions

Undefined vs null
Hello. What is the difference between undefined and null? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
November 19, 2019
javascript - Falsy values vs null, undefined, or empty string - Software Engineering Stack Exchange
I've worked with jQuery over the years. However, recently, I've found myself getting deeper into the JavaScript language. Recently, I've heard about "truthy" and falsey values. However, I don't fully More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
Null vs Undefined in JavaScript: Key Differences Explained - LambdaTest Community
What is the difference between null and undefined in JavaScript? Can you explain the concept of JavaScript null vs undefined? More on community.lambdatest.com
🌐 community.lambdatest.com
0
July 2, 2024
null vs undefined
I try to think of it simply as: undefined means exactly that, we have not bothered to set a value for this thing. null means we explicitly set a value and wanted it to be anomalous. null shows intentionality you cannot infer from undefined. More on reddit.com
🌐 r/javascript
43
132
June 21, 2017
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › undefined-vs-null-in-javascript
Undefined Vs Null in JavaScript - GeeksforGeeks
July 23, 2025 - undefined indicates a variable hasn’t been initialized, while null is intentionally assigned to indicate no value. Understanding the distinction helps write cleaner, more predictable code in JavaScript, especially when handling default values or checking for missing data.
🌐
Frank M Taylor
blog.frankmtaylor.com › 2023 › 05 › 25 › why-does-javascript-have-null-and-undefined
Why does JavaScript have null AND undefined? – Frank M Taylor
May 25, 2023 - null is for us to use, in our programs, to tell someone, “The object you wanted isn’t there; here’s an object you don’t want” · undefined is the default state of the JavaScript universe
🌐
CoreUI
coreui.io › blog › what-is-the-difference-between-null-and-undefined-in-javascript
What is the Difference Between Null and Undefined in JavaScript · CoreUI
February 9, 2025 - According to the typeof operator, however, typeof null returns 'object'—a quirk dating back to the original JavaScript implementation. If you see null console outputs it indicates a deliberate decision to assign null. This is different from a variable that is simply undefined.
🌐
SheCodes
shecodes.io › athena › 2227-what-is-the-difference-between-null-and-undefined-in-javascript
[JavaScript] - What is the Difference Between Null and Undefined in JavaScript?
Learn what is the difference between null and undefined in JavaScript and how to distinguish between them. Check how to use a typeof operator.
🌐
Hacker News
news.ycombinator.com › item
The difference between null and undefined in JavaScript is something I wished ha... | Hacker News
October 13, 2021 - I have only seen null vs undefined lead to 2 things in my experience: mistakes and bikeshedding · The "billon dollar mistake" as described by Tony Hoare was not nulls per se
Find elsewhere
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Undefined vs null - JavaScript - The freeCodeCamp Forum
November 19, 2019 - Hello. What is the difference between undefined and null?
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)
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Data_structures
JavaScript data types and data structures - JavaScript | MDN
The Null type is inhabited by exactly one value: null. The Undefined type is inhabited by exactly one value: undefined.
🌐
Scaler
scaler.com › topics › javascript › null-and-undefined-in-javascript
Null and Undefined in JavaScript - Scaler Topics
April 4, 2024 - Unlike null, the value of an undefined variable is set by JavaScript as undefined. The variable gets created at the run-time. When we do not pass an argument for a function parameter, the default value is taken as undefined.
🌐
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. And undefined it means that the variable wasn’t even declared(for example trying to print a variable ...
🌐
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.
🌐
Educative
educative.io › answers › null-versus-undefined-in-javascript
Null versus undefined in JavaScript
While undefined is a default state, null is a deliberate assignment to represent emptiness. Let’s explore null in detail. null is an object that represents an intentional absence of value.
🌐
DEV Community
dev.to › sduduzog › null-vs-undefined-what-to-choose-what-to-use-11g
null vs undefined? What to choose? What to use? - DEV Community
August 23, 2023 - When a javascript object is being serialized, all undefined properties are discarded, remember 'undefined' means a property is yet to be assigned a value. But null on the other hand is known by JSON as its a valid JSON data type
🌐
Syncfusion
syncfusion.com › blogs › post › null-vs-undefined-in-javascript
Null vs. Undefined in JavaScript | Syncfusion Blogs
December 10, 2024 - Some of the significant differences between null and undefined are: JavaScript always assigns undefined to indicate the absence of a value by default, and it never automatically assigns null as a value.
🌐
GitBook
basarat.gitbook.io › typescript › recap › null-undefined
Null vs. Undefined | TypeScript Deep Dive
August 18, 2020 - Something hasn't been initialized : undefined. Something is currently unavailable: null. Fact is you will need to deal with both. Interestingly in JavaScript with ==, null and undefined are only equal to each other:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
October 28, 2025 - Like undefined, null is treated as falsy for boolean operations, and nullish for nullish coalescing and optional chaining. The typeof null result is "object". This is a bug in JavaScript that cannot be fixed due to backward compatibility.
🌐
LambdaTest Community
community.lambdatest.com › general discussions
Null vs Undefined in JavaScript: Key Differences Explained - LambdaTest Community
July 2, 2024 - What is the difference between null and undefined in JavaScript? Can you explain the concept of JavaScript null vs undefined?
🌐
Medium
medium.com › @designtechworld › what-is-the-difference-between-null-and-undefined-in-javascript-fa8ad03938a3
What is the Difference Between Null and Undefined in ...
April 4, 2023 - In the above example, the variable is assigned a value of null, which indicates that it intentionally does not have any value. Unlike undefined, null is explicitly assigned to a variable, property, or method.