
null and undefined in JavaScript

A topic which frequently trips up even seasoned JavaScript developers is the differences and nuances between null and undefined which ‑ on the surface ‑ really ought to behave similarly (if not exactly the same).
What is null?
In JavaScript, null is a special value which you can specifically assign to a variable to show that it has no value (at least at that particular point in time).
let nullVariable = null;console.log(nullVariable); //=> nullHere, we are indicating that nullVariable exists, but at the moment, it is blank or empty. We have assigned it a value of nothing.
In order for a value to be set to null, it has to have been done manually (for example: if you are emptying a variable). JavaScript does not set a value to null itself.
What is undefined?
In contrast, undefined means that a variable has been set (i.e., it exists), but it has no assignment.
For example:
let undefinedVariable;console.log(undefinedVariable); //=> undefinedUnlike null, where the value is set explicitly, undefined is the default value assigned by JavaScript when a variable is declared at runtime but not (yet) assigned a value.
Differing typeof
One key difference between the two is when you attempt to check the type of a variable using typeof.
The type of null is object
When interrogating a null variable's type, it will return with object:
console.log(typeof null); //=> objectlet nullVariable = null;console.log(typeof nullVariable); //=> objectThe type of undefined is undefined
Unlike null, when you evaluate a variable that does not have an assigned value, the returned data type is undefined:
console.log(typeof undefined); //=> undefinedlet undefinedVariable;console.log(typeof undefinedVariable); //=> undefinedWhy is null an object?
It perhaps seems counter‑intuitive that null is a type of object, and there are many differing opinions. However, the most likely reason null is considered to be an object type may be because of a historical mistake in the design of the language.
The theory goes that when JavaScript was first created, its designers intended for null to represent an empty object pointer, which means that it should have been a primitive data type like undefined. However, the initial version of JavaScript stored values as 32 bits, with the first three bits representing the data type and the remaining bits containing the value. This meant that all object types started from 000. By its nature, null is empty so when read by JavaScript, it returns zeros and, hence is treated as an object.
This has been maintained ever since for backwards compatibility concerns, even though ‑ actually ‑ null is not an object, but rather a special value that represents the absence of any object value.
null and undefined in Arithmetic
The other big difference between the two is when it comes to using them in arithmetic (i.e., addition, subtraction, etc). When used in arithmetic operations, null is converted to 0, whereas undefined becomes NaN:
const number = 44;let nullNumber = null;let undefinedNumber;console.log(number + nullNumber); //=> 44console.log(number + undefinedNumber); //=> NaNThe Wrap‑Up
To summarise, the difference between null and undefined:
nullis an explicitly‑defined value, whereasundefinedis applied to variables that are declared, but have no value assigned to them.nullhas a type ofobject, whereasundefinedhas a type ofundefined.- When used in arithmetic,
nullconverts to0, whereasundefinedisNaN.
Fin.
Categories:
Related Articles

Exporting and Importing Using ES6 Modules. 
JavaScript's typeof Operator: Uses and Limitations. JavaScript's
typeofOperator: Uses and Limitations
Type Coercion in JavaScript: Implicit vs. Explicit Conversion. Type Coercion in JavaScript: Implicit vs. Explicit Conversion

Understanding WeakMap and WeakSet in JavaScript. Understanding
WeakMapandWeakSetin JavaScript
Getting Started with Callbacks in JavaScript. Getting Started with Callbacks in JavaScript

Enhancing User Experience with CSS and JavaScript Animations. Enhancing User Experience with CSS and JavaScript Animations

Interpolation: Sass Variables Inside calc(). Interpolation: Sass Variables Inside
calc()
Building a Custom Vue 3 Hook Using the Composition API. Building a Custom Vue 3 Hook Using the Composition API

Understanding the Difference Between <b> and <strong>. Understanding the Difference Between
<b>and<strong>
Understanding setTimeout() in JavaScript. Understanding
setTimeout()in JavaScript
Event Bubbling vs. Capturing in JavaScript. Event Bubbling vs. Capturing in JavaScript

CSS visibility: Hiding Elements Without Affecting Layout. CSS
visibility: Hiding Elements Without Affecting Layout