undefined vs null in JavaScript

Full stack engineer but passionnated by front-end Angular Expert / NX / JavaScript / Node / Redux State management / Rxjs
Search for a command to run...

Full stack engineer but passionnated by front-end Angular Expert / NX / JavaScript / Node / Redux State management / Rxjs
In JavaScript, undefined represents the value of a variable that wasnโt yet initialized, such as:
var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined
It is an assignment value, which is used with variable to represent no value.
var TestVar = null;
alert(TestVar); //shows null
alert(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.
Variables In JavaScript likes others languages we are using variables ! ๐ฑ They are containers for storing data values, in this article I will just explain how to use them, because the entire subject is a little bit hard, and at the beginning you don...
A structured approach to Angular applications with Ngrx Signal Store

The useEffect hook is fundamental to React functional components, but its behaviour changes dramatically based on how you handle the dependencies array. Let's explore the three main patterns and when to use each one. useEffect without dependencies ar...

Git worktree is a Git feature that allows you to check out multiple branches at once in different directories, without cloning the repository again. This powerful capability transforms how developers handle parallel development workflows. The Problem...

When working with Git in collaborative environments, you'll inevitably encounter situations where you need to overwrite remote history. Two commands come into play: git push --force and git push --force-with-lease. Understanding the difference betwee...

Ever needed to create a union type from an array of objects in TypeScript? There's an elegant solution using the [number] index signature notation that can save you from maintaining duplicate type definitions. The Problem Let's say you have an array ...
![TypeScript's [number] Index Signature: Extract Union Types from Arrays](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1752960594694%2Fe2be9492-869f-4169-bb1a-950ee81a3887.png&w=3840&q=75)
In last article I explained how to use variables in JavaScript but I also mentioned undefined, here I will try my best to explain to you what is undefined, what is the difference with null, why both are existing and I will give you some examples.
In JavaScript, undefined means a variable has been declared but has not yet been assigned a value or assigned undefined :
// console.warn(notDeclared); // Impossible not defined
console.warn(typeof notDeclared); // logs : undefined
let test;
console.warn(test); // logs : undefined
console.warn(typeof test); // logs : undefined
const testConst = undefined;
console.warn(testConst); // logs : undefined
console.warn(typeof testConst); // logs : undefined
// Impossible, missing initializer in const declaration
/* const testConst2;
console.warn(testConst2); // Broken code
console.warn(typeof testConst2); // Broken code */
Unlike undefined, null is an assignment value, null can be assigned to a variable as a representation of no value :
const test = null;
console.warn(test); // logs : null
console.warn(typeof test); // logs : object
As you can see undefined and null are very different undefined is a type itself, and null seems like an object but no it is in reality a primitive value
To understand why null is not an object I will quote a sentence from the book Professional JavaScript for Web Developers :* "You may wonder why the typeof operator returns 'object' for a value that is null. This was actually an error in the original JavaScript implementation that was then copied in ECMAScript. Today, it is rationalized that null is considered a placeholder for an object, even though, technically, it is a primitive value."*
Sometimes JavaScript is fun, or horrible depending on how you feel ๐ค
console.warn(null === undefined); // logs : false
console.warn(null == undefined); // logs : true
console.warn(null === null); // logs : true
// null = 'value'; // Impossible ReferenceError
// let undefined = 'value'; Impossible trick :
// Identifier 'undefined' has already been declared
// /!\ DON'T DO THIS /!\
(function () {
var undefined = 'wtf';
console.warn(undefined, typeof undefined); // logs : wtf string
})();
// /!\ DON'T DO THIS /!\
(function (undefined) {
console.warn(undefined, typeof undefined); // logs: wtf2 string
})('wtf2');
That's it for undefined and null !
๐ In my next article you can learn how to use conditions in JavaScript ๐