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
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › null-in-javascript
Null in JavaScript - GeeksforGeeks
June 5, 2024 - In JavaScript, `null` indicates the deliberate absence of any object value. It's a primitive value that denotes the absence of a value or serves as a placeholder for an object that isn't present.
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
You might define a variable as null with the expectation that it reflects either a value assigned to it at some point in a script or an explicitly absent value. You can also assign the null value to an existing reference to clear a previous 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.
Top answer
1 of 16
131

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
121

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 › Glossary › Null
Null - Glossary | MDN
In computer science, a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address. The meaning of a null reference varies among language implementations. In JavaScript, null is marked as one of the primitive values, because its behavior ...
🌐
Programiz
programiz.com › javascript › null-undefined
JavaScript null and undefined
Note: Usually, null is used to assign 'unknown' or 'empty' value to a variable. Hence, you can assign null to a variable. In JavaScript, null is a special value that represents an empty or unknown value.
🌐
Dmitri Pavlutin
dmitripavlutin.com › javascript-null
Everything about null in JavaScript
null in JavaScript is a special value that represents a missing object.
Find elsewhere
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › null
`null` in JavaScript - Mastering JS
In JavaScript, null is a value that represents the intentional absence of any object value. It is technically a primitive type, although in some cases it behaves as an object.
🌐
JavaScript Tutorial
javascripttutorial.net › home › an essential guide to javascript null
An Essential Guide to JavaScript null
September 29, 2020 - JavaScript uses the null value to represent the intentional absence of any object value. If you find a variable or a function that returns null, it means that the expected object couldn’t be created.
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-null-check
How to Check for Null in JavaScript | Built In
In JavaScript, null represents an intentional absence of a value, indicating that a variable has been declared with a null value on purpose. On the other hand, undefined represents the absence of any object value that is unintentional.
🌐
GeeksforGeeks
geeksforgeeks.org › undefined-vs-null-in-javascript
Undefined Vs Null in JavaScript | GeeksforGeeks
September 17, 2024 - It means null is equal to undefined but not identical. 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 initialized, while null is intentionally assigned to indicate no 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 ...
🌐
Scaler
scaler.com › home › topics › javascript › null and undefined in javascript
Null and Undefined in JavaScript - Scaler Topics
April 4, 2024 - When we assign null as a value to any variable, it means that it is empty or blank. It is to show that the variable has no value. Also, null is an object in JavaScript. When it gets assigned to a variable, it represents no value.
🌐
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
November 24, 2025 - All are objects. so I think Brendan ... everything is an object, JavaScript’s null actually has a more specific meaning of “no object”....
🌐
Reddit
reddit.com › r/javascripttips › why does javascript have both null and undefined?
r/JavaScriptTips on Reddit: Why does JavaScript have both null and undefined?
November 11, 2022 -

Most programming languages have a single value to indicate the absence of something, which is often called null and is used to represent a variable that has no value associated with it.

But JavaScript is different. Someone who is just starting out with JavaScript or coming from a different language usually finds it hard to understand, why there are two values that indicate absence: null and undefined

Check out the post to learn how these two are different.

🌐
Medium
medium.com › @stephenthecurt › a-brief-history-of-null-and-undefined-in-javascript-c283caab662e
A brief history of Null and Undefined in JavaScript | by Steve C | Medium
January 13, 2018 - Just like numbers and characters, ... is `null`. Same with `undefined`. These are used in JavaScript to act as placeholders to let the programmer know when a variable has no value....
🌐
To The New
tothenew.com › home › difference between ‘null’ and ‘undefined’ in javascript
Difference Between ‘null’ and ‘undefined’ in JavaScript | TO THE NEW Blog
May 23, 2013 - An illustration of this assignment ... ... ‘Null’ is an object with a valid value, no properties, is non-mutable, and only a single instance of the same exists in the system at all times....
🌐
Medium
medium.com › weekly-webtips › null-and-undefined-in-javascript-d9bc18acdaff
Null and Undefined in Javascript. What’s the difference between null and… | by Megan Lo | Webtips | Medium
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 .