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
๐ŸŒ
web.dev
web.dev โ€บ learn โ€บ javascript โ€บ data-types โ€บ null-undefined
null and undefined | web.dev
Unlike the reserved keyword null, undefined is a property of the global object. This was a design decision made early in JavaScript's development, and it let legacy browsers overwrite undefined completely. In modern browsers, it's still possible to use undefined as an identifier in non-global scopes, overriding its value within the scope of that declaration.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Operators โ€บ null
null - JavaScript | MDN
The null keyword refers to the null primitive value, which represents the intentional absence of any object value. function getVowels(str) { const m = str.match(/[aeiou]/gi); if (m === null) { return 0; } return m.length; } console.log(getVowels("sky")); // Expected output: 0 ... The keyword ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ undefined-vs-null-in-javascript
Undefined Vs Null in JavaScript - GeeksforGeeks
July 23, 2025 - When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty. undefined indicates a variable hasnโ€™t been ...
๐ŸŒ
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.
๐ŸŒ
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 - undefined is a primitive value used when a variable has not been assigned a value. This is just a JavaScript thing. With JavaScript you can travel back in time to make a variable not assigned a value.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ undefined versus null in javascript
Undefined versus null in JavaScript | Sentry
console.log(undeclaredVar); // will throw a ReferenceError console.log(typeof undeclaredVar); // will print "undefined" In contrast, null is a value that represents nothing. Think of null as an empty container and undefined as the absence of ...
๐ŸŒ
CodeBurst
codeburst.io โ€บ javascript-null-vs-undefined-20f955215a2
JavaScript โ€” Null vs. Undefined
January 16, 2018 - Hereโ€™s an example. We assign the value of null to a: ... Undefined most typically means a variable has been declared, but not defined. For example: ... In JavaScript there are only six falsy values. Both null and undefined are two of the six falsy values.
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ javascript โ€บ null-and-undefined-in-javascript
Null and Undefined in JavaScript - Scaler Topics
April 21, 2022 - Null in JavaScript means an empty value and is also a primitive type in JavaScript. The variable which has been assigned as null contains no value. Undefined, on the other hand, means the variable has been declared, but its value has not been assigned.
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ undefined-vs-null-javascript
Undefined vs Null - Javascript - Flexiple
Simply put, undefined means a variable has been declared but has not yet been assigned a value. undefined is a type by itself (undefined). Unassigned variables are initialized by JavaScript with a default value of 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.
๐ŸŒ
Syncfusion
syncfusion.com โ€บ blogs โ€บ post โ€บ null-vs-undefined-in-javascript
Null vs. Undefined in JavaScript | Syncfusion Blogs
December 10, 2024 - Both are primitive JavaScript values, meaning they are not considered objects and therefore donโ€™t have methods or properties. Both are false when encountered in a Boolean context (falsy values). Despite both being falsy in a Boolean context, null or undefined is not loosely equal (==) to any other falsy value such as zero (0), an empty string (โ€ โ€œ, โ€˜ โ€˜), or Not-a-Number (NaN).
๐ŸŒ
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?

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ 'null' or 'undefined': what should i use if i want to clear the variable from the memory?
r/learnjavascript on Reddit: 'null' or 'undefined': What should I use if I want to clear the variable from the memory?
June 7, 2023 -

Please consider the following:

var myFruits = ['Banana', 'Apple', 'Strawberry'];
// SOME CODING
// SOME CODING
myFruits = undefined; // Is this better?
myFruits = null; // or is this better?

Further question, what is the distinction between the two? Is there any cases where only null is used or undefined is used? Thanks.

Top answer
1 of 3
5
As NateDzMtz says, the memory considerations are the same. null and undefined are unique values and don't involve any references into the heap. In this regard, false would have the same effect. As far as which is convenient for programming, since indexing an object with a key that is not found in the object returns undefined, storing undefined as the value almost simulates absence of the key. Of course, a query can be made to distinguish the case that foo has no key bar from the case where it has the key bar but undefined is stored as the value at that key. But if your design is such that those cases don't have different meanings, it's convenient to stifle slots by putting undefined in them. Note that delete can be inefficient in some engines and they are not required by the standard to make it efficient. I think that the conventional meanings of the special values are, more or less: undefined -- maybe was never initialized; isn't associated to any particular data type. null -- no object, where an object might be expected. NaN -- no number, where a number might be expected. false -- just not true, no other meaning. Note that typeof null is "object", even though you can't index null. typeof undefined is "undefined". typeof NaN is "number", even though NaN explicitly and exactly means "Not a Number"!
2 of 3
5
In my opinion I typically would use null to denote the absence of the variable for purposes of debugging. It helps with identifying that the variable was intentionally set to a null value as to not be confused with the variable not being defined in the first place. Additionally, using null can be useful when you want to explicitly assign a "no value" state to a variable. This can be helpful in scenarios where you want to differentiate between an intentional absence of a value and a variable that has not been assigned any value yet. On the other hand, undefined is often used by JavaScript itself to indicate that a variable has been declared but has not been assigned any value. It is the default value for uninitialized variables. In most cases, you don't need to explicitly set a variable to undefined because JavaScript does it automatically. However, it's worth noting that both null and undefined have similar behaviors when it comes to memory management. Assigning either of them to a variable will release the memory occupied by the previous value and make the variable eligible for garbage collection. In conclusion, while both null and undefined can be used to clear a variable from memory, null is typically preferred when you want to denote an intentional absence of value, while undefined is automatically assigned by JavaScript when a variable is declared but not assigned a value.
๐ŸŒ
TutorialsTeacher
tutorialsteacher.com โ€บ javascript โ€บ javascript-null-and-undefined
Difference between null and undefined in JavaScript
Here you will learn what is null and undefined in JavaScript and what is the difference between them. A null means the absence of a value. You assign a null to a variable with the intention that currently this variable does not have any value ...
๐ŸŒ
Web Dev Simplified
blog.webdevsimplified.com โ€บ 2021-01 โ€บ null-vs-undefined
Null Vs Undefined
If a variable is null then it means the variable has no value and that it was explicitly set to have no value by the programmer. A variable will never be null unless somewhere in the code a programmer set a variable to null.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ null-undefined
JavaScript null and undefined
In JavaScript, when you pass undefined to a function parameter that takes a default value, the undefined is ignored and the default value is used. For example, function test(x = 1) { console.log(x); } // passing undefined // takes default value 1 test(undefined); // 1 ยท However, when you pass null to a default parameter function, the function takes the null as a value.
๐ŸŒ
Medium
medium.com โ€บ weekly-webtips โ€บ null-and-undefined-in-javascript-d9bc18acdaff
Null and Undefined in Javascript
February 17, 2021 - Interesting thing about null is that null expresses the lack of identification, meaning the variable points to no object. A variable that has not been assigned a value is considered a type of undefined .
๐ŸŒ
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 | Blog
1 month ago - 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).