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
🌐
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.
Discussions

types - Should a language have both null and undefined values? - Programming Language Design and Implementation Stack Exchange
Javascript for example has both null and undefined values. They are nearly equivalent: both are falsy values that denote absence of data, the only difference is apparently whether or not it was " More on langdev.stackexchange.com
🌐 langdev.stackexchange.com
Undefined vs null
Hello. What is the difference between undefined and null? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
8
0
November 19, 2019
Undefined vs null
One reason to favor undefined over null is how javascript handle default values: const withDefault = (a = true) => { console.log(a); }; withDefault(); // logs true withDefault(undefined); // logs true withDefault(null); // logs null More on reddit.com
🌐 r/typescript
51
46
February 27, 2023
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
133
January 13, 2018
🌐
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?

🌐
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
🌐
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.
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
You can also assign the null value ... value. undefined is a primitive value assigned to variables that have just been declared, or to the resulting value of an operation that doesn't return a meaningful value....
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
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.
🌐
Quora
quora.com › Is-there-any-serious-performance-difference-between-using-undefined-or-null-as-a-dummy-value-in-JS
Is there any serious performance difference between using undefined or null as a dummy value in JS? - Quora
Answer (1 of 6): As others have already mentioned, it really depends on the “serious” threshold you’re considering, your JavaScript runtime environment, and how often you depend on [code ]undefined[/code] or [code ]null[/code]. That last bit is important in a non-obvious way: The JavaScript engi...
🌐
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 a th...
🌐
Medium
medium.com › @o_o_o › null-vs-undefined-can-i-use-only-one-a3b7db5468f2
null vs. undefined: Can I use only one? | by OOO | Medium
March 29, 2022 - It is even considered as design mistake by JavaScript’s creator, Brendan Eich. So I wanted to simplify things by having only one non-value. The biggest downside of only using null is that undefined is everywhere.
🌐
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
🌐
Medium
vvkchandra.medium.com › essential-javascript-mastering-null-vs-undefined-66f62c65d16b
Essential JavaScript: Mastering null vs undefined | by Chandra Gundamaraju | Medium
September 9, 2020 - Before we get to null and undefined, let’s understand what happens when we use a variable that’s never declared. On line 4, the variable universe does not exist. If you run this code in the console, you will see an error like the following: ... That’s fair given we haven’t declared universe anywhere. This is what we mean by undeclared variables. JavaScript engine throws a ReferenceError when it’s unable to find an identifier in all of the accessible scopes.
Top answer
1 of 7
26

both are falsy values that denote absence of data, the only difference is apparently whether or not it was "intentional"

The main problem with Javascript having both null and undefined is that they are used inconsistently, even in standardised APIs. There is no absolute authority which says what each should be used to mean or indicate, and so different programmers imbue them with different meanings. Library code mostly has to treat them the same way, because there is no agreed standard for how they should be treated differently.

Perhaps to you, null means an "intentionally" missing value and undefined means an "unintentionally" missing value, but this isn't universal. The ECMAScript specification doesn't seem to have anything to say on the subject. MDN advises that null means "the intentional absence of any object value" but doesn't give the opposite advice for undefined. And as an example, canvas.getContext('2d') returns null when the same canvas is already in use in a 3D context, something very unlikely to be done intentionally.

Worse, "intentionally missing" isn't as clear a distinction as you might think. For example, function parameters take the value undefined when no argument is provided, but an argument could be missing either intentionally or unintentionally. Likewise, map.get(missingKey) returns undefined, but the key could be absent either intentionally or unintentionally. (Meanwhile, localStorage.getItem returns null instead in the same case.) The intent where a value is produced may also be different than where it is used.

As a result, if you come across some variable which might hold either an object or null or undefined, you will have to read the comments (hopefully there are some!) to figure out what the author intended by that. Perhaps the author didn't even intend to treat them differently, just there is a mix of undefined and null because those values came from different places and there was no need to unify them.

If instead there was a language-wide authoritative standard which said undefined means one thing and null means another, then this would be less of a problem, although there would still be code which doesn't follow the standard. That said, if you can think of a clear difference between what these two values should mean in your language, then you can probably think of better names for these values which would communicate those meanings.

2 of 7
7

It is useful but not necessary and there are better alternatives.

Dynamically typed languages have the limitations that they can only discover concrete types of values. That means they cannot differentiate between levels of a value being undefined: a function taking a value: Option[T] = None cannot find out whether it received the default None or was passed one if T is itself some Option[T'].

This can be practically fixed by having more than one "not defined" value: A function taking a value: T = undef can trivially separate the case of default undef and passed in None.
Of course this only shifts the problem: While for many practical problems eliminating half the collisions is enough, collisions still occur. A function that has to operate on both default and passed in undef/None still cannot separate the cases.

A more thorough approach is to allow/encourage defining new "not defined" values/types for each use. For example, Python has the idiom of using a hidden, bare object() as needed.

# placeholder with unique identity
_no_default = object()

def find(predicate, values, default = _no_default):
   ...

Of course a language can provide tools and syntactic sugar to make this simpler.

🌐
Harness
harness.io › blog › feature management & experimentation › why you should use undefined instead of null in javascript
Why You Should Use Undefined Instead of Null in Javascript
January 21, 2026 - The blog outlines best practices and edge cases, concluding that `undefined` provides cleaner and more predictable results. JavaScript is an unusual programming language in that it has two null-like values: undefined and null. These are distinct values (null !== undefined).
🌐
Syncfusion
syncfusion.com › blogs › javascript › 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.
🌐
DEV Community
dev.to › johannesjo › how-do-you-deal-with-null-vs-undefined-4899
How do you deal with null vs undefined? - DEV Community
July 5, 2020 - So, I would use undefined whenever a variable has not been assigned yet, or when a method or function does not return anything (exactly how the language behaves on runtime). On the other hand, null is used when you explicitly say that variable ...
🌐
C# Corner
c-sharpcorner.com › article › what-is-the-difference-between-null-and-undefined-in-javascript
What is the difference between 'null' and 'undefined' in JavaScript?
April 4, 2023 - In summary, undefined indicates a variable has not been assigned a value or a function does not have a return value. On the other hand, null indicates a variable or object property has no value.
🌐
GitConnected
levelup.gitconnected.com › javascript-null-vs-undefined-2acda986f79f
JavaScript Null vs Undefined. Understanding the difference between… | by Aiman Rahmat | Level Up Coding
January 30, 2020 - Undefined is a variable that has been declared but not assigned a value. Null as an assignment value. So you can assign the value null to any variable which basically means it’s blank. So by not declaring a value to a variable, JavaScript automatically assigns the value to undefined.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Undefined vs null - JavaScript - The freeCodeCamp Forum
November 19, 2019 - Hello. What is the difference between undefined and null?