I don't really have an answer, but according to Nicholas C. Zakas, page 30 of his book "Professional JavaScript for Web Developers":

When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to null as opposed to anything else. That way, you can explicitly check for the value null to determine if the variable has been filled with an object reference at a later time

Answer from bbg on Stack Overflow
🌐
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 - Lets say the form is for user details and the fields are "name", "age", "height". ... Property 'age' exists and has number. Update age with provided integer · Property 'name' is not defined. Do nothing · Property 'height' is null. Clear its field in the data store. How does the backend figure out that the property 'name' is not defined? Because it isn't. That payload is equivalent to the following · const body = { age: 24, height: null, name: undefined }
🌐
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?

Top answer
1 of 16
130

I don't really have an answer, but according to Nicholas C. Zakas, page 30 of his book "Professional JavaScript for Web Developers":

When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to null as opposed to anything else. That way, you can explicitly check for the value null to determine if the variable has been filled with an object reference at a later time

2 of 16
120

At the end of the day, because both null and undefined coerce to the same value (Boolean(undefined) === false && Boolean(null) === false), you can technically use either to get the job done. However, there is right way, IMO.

  1. Leave the usage of undefined to the JavaScript compiler.

    undefined is used to describe variables that do not point to a reference. It is something that the JS compiler will take care for you. At compile time the JS engine will set the value of all hoisted variables to undefined. As the engine steps through the code and values becomes available the engine will assign respective values to respective variables. For those variables for whom it did not find values, the variables would continue to maintain a reference to the primitive undefined.

  2. Only use null if you explicitly want to denote the value of a variable as having "no value".

    As @com2gz states: null is used to define something programmatically empty. undefined is meant to say that the reference is not existing. A null value has a defined reference to "nothing". If you are calling a non-existing property of an object, then you will get undefined. If I would make that property intentionally empty, then it must be null so you know that it's on purpose.

TLDR; Don't use the undefined primitive. It's a value that the JS compiler will automatically set for you when you declare variables without assignment or if you try to access properties of objects for which there is no reference. On the other hand, use null if and only if you intentionally want a variable to have "no value".


Sidebar: I, personally, avoid explicitly setting anything to undefined (and I haven't come across such a pattern in the many codebases/third party libs I've interacted with). Also, I rarely use null. The only times I use null is when I want to denote the value of an argument to a function as having no value, i.e.,:

function printArguments(a,b) {
  console.log(a,b);
}

printArguments(null, " hello") // logs: null hello
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
The null keyword refers to the ... The keyword null is a literal for the null value. Unlike undefined, which is a global variable, null is not an identifier but a syntax keyword....
🌐
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....
🌐
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 - If you create useRef with undefined, Typescript assumes that it’s a value container and returns type of MutableRefObject<T>. This doesn’t match the type that element’s ref property is expecting and throws error. Typescript returns RefObject<T> only if useRef is initialized with null. To find out more why it is designed this way, read this and this.
🌐
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.
Find elsewhere
🌐
Flexiple
flexiple.com › javascript › undefined-vs-null-javascript
Undefined vs Null - Javascript - Flexiple
As you can see here, both, variable t which is undefined and variable a which is see to null, return false and hence do not satisfy either of the if conditions and return the output "both t and a are not defined with a value". Let’s see what happens when you compare undefined and null using the JavaScript equality operators. // comparing undefined and null undefined == null; //true undefined === null; //false · As you can see, when the equality operator is used it compares only the values.
🌐
Scaler
scaler.com › topics › javascript › null-and-undefined-in-javascript
Null and Undefined in JavaScript - Scaler Topics
April 21, 2022 - The difference between null and ... division etc. While performing various arithmetic operations, null is converted to 0, and then the operation is performed, while undefined is converted to NaN....
🌐
Syncfusion
syncfusion.com › blogs › post › null-vs-undefined-in-javascript
Null vs. Undefined in JavaScript | Syncfusion Blogs
December 10, 2024 - The following instances depict ... to uninitialized variables, if you want to indicate the absence of a deal explicitly, always use null instead of undefined to avoid confusion....
🌐
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.
🌐
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 - When you assign null, you are specifying that no meaningful value exists at the moment, but the variable has been declared so it can hold future data. Also, note that unlike null, undefined usually arises without explicit action from the developer.
🌐
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 - This means that if you do allow null into your data flows, when you do typeof on any variable that might receive null, you must check whether an object result means null or some form of {…}. Similarly, if you are checking whether something is an object, you must then make sure it isn’t a null before you dereference it. undefined has none of these problems, since typeof myValue === "undefined" is not ambiguous. If you use TypeScript and use it with a great deal of discipline, it is much more realistic to manage both null and undefined flowing through your application.
🌐
Codedamn
codedamn.com › news › node.js
Null vs Undefined in JavaScript and Node.js
May 22, 2023 - On the other hand, undefined is typically used by JavaScript itself to indicate that a variable has not been assigned a value. For example, when you want to represent an empty object property, it’s better to use null:
🌐
Web Dev Simplified
blog.webdevsimplified.com › 2021-01 › null-vs-undefined
Null Vs Undefined
If no entry exists it makes the most sense to return null since you are stating that there is no value found. On the other hand undefined means that there is no value because no value has been set yet.
🌐
GitBook
basarat.gitbook.io › typescript › recap › null-undefined
Null vs. Undefined | TypeScript Deep Dive
Since attributes set to null are ... value to null before encoding and transmitting the object to a remote store. Setting attribute values to undefined can save on storage and transmission costs, as the attribute names will not be encoded. However, this can complicate the semantics of clearing values vs. absent values. TypeScript team doesn't use null : TypeScript ...
🌐
Writing JavaScript
writingjavascript.com › why-you-should-always-use-undefined-and-never-null
Why you should always use undefined, and never null - Writing JavaScript
When a variable or a property on an object does not have a value it is undefined, and for a value to be null you need to assign it a null value. As the variable already has no value there is absolutely no point in declaring that again with another ...
🌐
Bits Kingdom
bitskingdom.com › home › development › what’s the difference between null and undefined in javascript?
Difference Between null and undefined: A JavaScript Guide
December 16, 2024 - Avoid explicitly setting a variable ... cases. undefined: Automatically assigned by JavaScript for uninitialized variables or non-existent properties. null: An intentional assignment to indicate “no value” or “empty.”...
Price   $$
Address   3235 Satellite Blvd Building 400 Suite 550, 30096, Duluth
🌐
DEV Community
dev.to › typescripttv › what-is-the-difference-between-null-and-undefined-5h76
What is the difference between null and undefined? - DEV Community
March 12, 2023 - The below function shows how null ... } else { return { error: null, result: a / b }; } } On the other hand, undefined represents the absence of any value....