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
🌐
MakeUseOf
makeuseof.com › home › programming › what's the difference between null and undefined in javascript?
What's the Difference Between Null and Undefined in JavaScript?
June 15, 2021 - In JavaScript, null is a primitive value that is used to signify the intentional absence of an object value, whereas undefined is a primitive value that acts as a placeholder for a variable that has not been assigned a value.
Discussions

'null' or 'undefined': What should I use if I want to clear the variable from the memory?
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"! More on reddit.com
🌐 r/learnjavascript
9
4
June 7, 2023
How do null and undefined differ in JavaScript programming?
I’m working on a JavaScript project and I keep running into situations where I encounter both null and undefined values. I’m getting confused about when each one appears and what they actually mean. For example, when I create variables or work with object properties, sometimes I see one ... More on community.latenode.com
🌐 community.latenode.com
1
0
June 2, 2025
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
132
March 26, 2017
🌐
Gabrielcordeiro
gabrielcordeiro.dev › blogs › what's the difference between null, undefined and empty in javascript
What's the difference between Null, Undefined and Empty in Javascript | Gabriel Schmidt Cordeiro
February 16, 2024 - Interestingly, null and undefined are loosely equal (null == undefined), which means they are considered equal in comparisons using the loose equality operator (==). However, they are strictly unequal (null !== undefined), meaning they are not ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript
JavaScript | MDN
JavaScript's dynamic capabilities include runtime object construction, variable parameter lists, function variables, dynamic script creation (via eval), object introspection (via for...in and Object utilities), and source-code recovery (JavaScript functions store their source text and can be retrieved through toString()).
🌐
CodeParrot
codeparrot.ai › blogs › javascript-null-vs-undefined-key-differences-when-to-use-each
JavaScript Null vs Undefined: Key Differences & When to Use Each
November 8, 2024 - In JavaScript, both null and undefined indicate "no value," but they serve different roles. When compared using loose equality (==), JavaScript considers null and undefined to be loosely equal, as they both imply an absence.
🌐
Oreate AI
oreateai.com › blog › understanding-the-distinction-between-null-and-undefined-in-javascript › 64a7d20d5c5850a50d4f0e9d7cf5c775
Understanding the Distinction Between Null and Undefined in JavaScript - Oreate AI Blog
January 15, 2026 - Here, the variable username is declared but remains uninitialized—hence its state as undefined. On the other hand, null represents an intentional absence of any object value. Think of it as actively choosing to leave a space empty rather than ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Data types
July 9, 2024 - If a variable is declared, but ... Normally, one uses null to assign an “empty” or “unknown” value to a variable, while undefined is reserved as a default initial value for unassigned things....
Find elsewhere
🌐
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.
🌐
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
🌐
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.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › narrowing.html
TypeScript: Documentation - Narrowing
JavaScript’s looser equality checks with == and != also get narrowed correctly. If you’re unfamiliar, checking whether something == null actually not only checks whether it is specifically the value null - it also checks whether it’s potentially undefined.
🌐
DEV Community
dev.to › icncsx › js-explain-undefined-null-and-nan-2lka
JS / explain undefined, null, and NaN - DEV Community
May 28, 2020 - Unlike undefined, null is never assumed by default. Null is typically used when we want to reset a variable; this is typically more clear than reassigning a variable to an empty string, 0, or undefined.
🌐
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.
🌐
Flexiple
flexiple.com › javascript › undefined-vs-null-javascript
Undefined vs Null - Javascript - Flexiple
March 10, 2022 - 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.
🌐
W3Schools
w3schools.com › typescript › typescript_null.php
TypeScript Null & Undefined
It allows writing expressions that have a fallback specifically when dealing with null or undefined. This is useful when other falsy values can occur in the expression but are still valid. It can be used with the ?? operator in an expression, similar to using the && operator. function printMileage(mileage: number | null | undefined) { console.log(`Mileage: ${mileage ??
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
JavaScript has two primitive values used to signal absent or uninitialized value: null and undefined.
🌐
Node.js
nodejs.org › api › fs.html
File system | Node.js v25.7.0 Documentation
Returns: <Promise> Fulfills with undefined upon success. Creates a symbolic link. The type argument is only used on Windows platforms and can be one of 'dir', 'file', or 'junction'. If the type argument is null, Node.js will autodetect target type and use 'file' or 'dir'.
🌐
Latenode
community.latenode.com › other questions › javascript
How do null and undefined differ in JavaScript programming? - JavaScript - Latenode Official Community
June 2, 2025 - I’m working on a JavaScript project and I keep running into situations where I encounter both null and undefined values. I’m getting confused about when each one appears and what they actually mean. For example, when I create variables or work with object properties, sometimes I see one ...
🌐
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?