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
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
For example, this can happen when you declare a function in a browser's developer console: ... A function explicitly returns undefined when its return statement returns no value. ... Although undefined and null have some functional overlap, they have different purposes.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
Semantically, their difference is very minor: undefined represents the absence of a value, while null represents the absence of an object. For example, the end of the prototype chain is null because the prototype chain is composed of objects; document.querySelector() returns null if it doesn't ...
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-null-and-undefined
Difference between null and undefined in JavaScript
The null and undefined variables are falsy to if-statements and ternary operators. Example: Null and undefined with if-statements Copy
🌐
CodeBurst
codeburst.io › javascript-null-vs-undefined-20f955215a2
JavaScript — Null vs. Undefined
January 16, 2018 - We assign the value of null to a: ... Undefined most typically means a variable has been declared, but not defined. For example: ... In JavaScript there are only six falsy values. Both null and undefined are two of the six falsy values.
🌐
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.
🌐
Flexiple
flexiple.com › javascript › undefined-vs-null-javascript
Undefined vs Null - Javascript - Flexiple
Here as the variable is declared but not assigned to any value, the variable by default is assigned a value of undefined. On the other hand, null is an object. It can be assigned to a variable as a representation of no value. JavaScript never sets a value to null. That must be done programmatically. For example, when you do not know the value initially you can assign a null to a variable.
🌐
Syncfusion
syncfusion.com › blogs › post › null-vs-undefined-in-javascript
Null vs. Undefined in JavaScript | Syncfusion Blogs
December 10, 2024 - Since undefined is the default value assigned by JavaScript to uninitialized variables, if you want to indicate the absence of a deal explicitly, always use null instead of undefined to avoid confusion. To check if a variable has any value before proceeding further in a program, you can use the loose equality ==null to check for either null or undefined.For example, in the following program, the function assignVal() checks whether the num is undefined or null and assigns the value given by the user only if the variable num is not initialized to any value.
🌐
TutorialsPoint
tutorialspoint.com › What-is-the-difference-between-null-and-undefined-in-JavaScript
What is the difference between null and undefined in JavaScript?
In JavaScript, use null to explicitly indicate that a variable has no value or is intentionally empty. Use undefined when something is naturally missing, such as an uninitialized variable or a non-existing object property.
Find elsewhere
🌐
Programiz
programiz.com › javascript › null-undefined
JavaScript null and undefined
For example, let name = "Felix"; // assigning undefined to the name variable name = undefined console.log(name); // returns 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 ...
🌐
Scaler
scaler.com › topics › javascript › null-and-undefined-in-javascript
Null and Undefined in JavaScript - Scaler Topics
April 21, 2022 - The setting of the value must be done manually by the user as JavaScript never sets the value as null. An object can be emptied by setting it to null. ... Here we have assigned the value null to variable x. There is a subtle difference between null and undefined, but as a programmer, it is important that we understand it clearly.
🌐
Exploring JS
exploringjs.com › js › book › ch_undefined-null.html
The non-values undefined and null • Exploring JavaScript (ES2025 Edition)
> getProp(undefined) TypeError: Cannot read properties of undefined (reading 'prop') > getProp(null) TypeError: Cannot read properties of null (reading 'prop') > getProp(true) undefined > getProp({}) undefined · In Java (which inspired many aspects of JavaScript), initialization values depend on the static type of a variable: Variables with object types are initialized with null. Each primitive type has its own initialization value. For example, int variables are initialized with 0.
Top answer
1 of 6
125

I find that some of these answers are vague and complicated, I find the best way to figure out these things for sure is to just open up the console and test it yourself.

var x;

x == null            // true
x == undefined       // true
x === null           // false
x === undefined      // true

var y = null;

y == null            // true
y == undefined       // true
y === null           // true
y === undefined      // false

typeof x             // 'undefined'
typeof y             // 'object'

var z = {abc: null};

z.abc == null        // true
z.abc == undefined   // true
z.abc === null       // true
z.abc === undefined  // false

z.xyz == null        // true
z.xyz == undefined   // true
z.xyz === null       // false
z.xyz === undefined  // true

null = 1;            // throws error: invalid left hand assignment
undefined = 1;       // works fine: this can cause some problems

So this is definitely one of the more subtle nuances of JavaScript. As you can see, you can override the value of undefined, making it somewhat unreliable compared to null. Using the == operator, you can reliably use null and undefined interchangeably as far as I can tell. However, because of the advantage that null cannot be redefined, I might would use it when using ==.

For example, variable != null will ALWAYS return false if variable is equal to either null or undefined, whereas variable != undefined will return false if variable is equal to either null or undefined UNLESS undefined is reassigned beforehand.

You can reliably use the === operator to differentiate between undefined and null, if you need to make sure that a value is actually undefined (rather than null).

According to the ECMAScript 5 spec:

  • Both Null and Undefined are two of the six built in types.

4.3.9 undefined value

primitive value used when a variable has not been assigned a value

4.3.11 null value

primitive value that represents the intentional absence of any object value

2 of 6
81

The DOM methods getElementById(), nextSibling(), childNodes[n], parentNode() and so on return null (defined but having no value) when the call does not return a node object.

The property is defined, but the object it refers to does not exist.

This is one of the few times you may not want to test for equality-

if(x!==undefined) will be true for a null value

but if(x!= undefined) will be true (only) for values that are not either undefined or null.

🌐
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 - According to the typeof operator, however, typeof null returns 'object'—a quirk dating back to the original JavaScript implementation. If you see null console outputs it indicates a deliberate decision to assign null. This is different from a variable that is simply undefined.
🌐
Medium
medium.com › weekly-webtips › null-and-undefined-in-javascript-d9bc18acdaff
Null and Undefined in Javascript
February 17, 2021 - Quick Note: As mentioned earlier, null and undefined are both primitive values, but interestingly enough, if we test out in typeof , they gave us different result: All other values in Javascript are objects ({}, [], functions…). So, this is generally regarded as a mistake when Javascript is first created.
🌐
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.
🌐
Medium
designtechworld.medium.com › what-is-the-difference-between-null-and-undefined-in-javascript-fa8ad03938a3
What is the Difference Between Null and Undefined in JavaScript? | by Sumit kumar Singh | Medium
April 4, 2023 - It is often used to assign an empty or non-existent value to an object or a variable. let variable = null; console.log(variable); // null · In the above example, the variable is assigned a value of null, which indicates that it intentionally ...
🌐
Sentry
sentry.io › sentry answers › javascript › undefined versus null in javascript
Undefined versus null in JavaScript | Sentry
console.log(undeclaredVar); // will throw a ReferenceError console.log(typeof undeclaredVar); // will print "undefined" In contrast, null is a value that represents nothing. Think of null as an empty container and undefined as the absence of ...
🌐
Programiz
programiz.com › javascript › examples › check-undefined-null
JavaScript Program To Check If A Variable Is undefined or null
To understand this example, you ... function checkVariable(variable) { if(variable == null) { console.log('The variable is undefined or null'); } else { console.log('The variable is neither undefined nor null'); } } let newVariable; checkVariable(5); checkVariable('hello'); checkVariable(null); checkVariable(newVariable);...
🌐
CodingNomads
codingnomads.com › null-undefined-difference-javascript
Null vs Undefined: Understanding the Differences in JavaScript
The code example demonstrates a potential use of null. You initialize a variable with null to denote that it is intentionally empty. After attempting to find a match, if the variable remains null, you know the function didn’t find what it was looking for. ... When you create a variable without a value, Javascript automatically assigns it the value of undefined...