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
🌐
Beyondjava
beyondjava.net › null-vs-undefined
Null vs. undefined
Beyond Java · Sitemap · Talks & Articles · Projects · Guest Posts · About · Legalese · Statistics · (opt out) · Mastodon
🌐
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 - When a javascript object is being serialized, all undefined properties are discarded, remember 'undefined' means a property is yet to be assigned a value. But null on the other hand is known by JSON as its a valid JSON data type
🌐
Quora
quora.com › What-is-the-main-difference-between-NULL-and-UNDEFINED-in-programming-Are-they-both-non-existent-values
What is the main difference between 'NULL' and 'UNDEFINED' in programming? Are they both 'non-existent values'? - Quora
Answer (1 of 9): An undefined variable does not exist at all. This error usually occurs when you attempt to access a variable before it has even been created. On the other hand, a NULL variable exists, but holds a value of zero/nothing.
🌐
web.dev
web.dev › learn › javascript › data-types › null-undefined
null and undefined | web.dev
... You might define a variable ... also assign the null value to an existing reference to clear a previous value. undefined is a primitive ......
🌐
Coders Campus
coderscampus.com › home › ep37 – null vs undefined in javascript
EP37 - Null vs Undefined in JavaScript - Coders Campus
April 9, 2021 - One thing that threw me off when I started learning JavaScript (with a background in Java) was the fact that JavaScript has both undefined and null as possible values for a variable. In Java, all we have is just null which means that
🌐
Hacker News
news.ycombinator.com › item
The difference between null and undefined in JavaScript is something I wished ha... | Hacker News
October 13, 2021 - I have only seen null vs undefined lead to 2 things in my experience: mistakes and bikeshedding · The "billon dollar mistake" as described by Tony Hoare was not nulls per se
Find elsewhere
🌐
CodeBurst
codeburst.io › javascript-whats-the-difference-between-null-undefined-37793b5bfce6
JavaScript — What’s the difference between Null & Undefined? | by Brandon Morelli | codeburst
July 5, 2017 - JavaScript — What’s the difference between Null & Undefined? Null and Undefined have a few subtle differences. Learn the details in two minutes. Preface Hey! I’m Brandon. I created codeburst.io …
🌐
Javatpoint
javatpoint.com › null-vs-undefined
Difference Between Null and Undefined - javatpoint
Difference Between Null and Undefined with typescript tutorial, typescript introduction, versions, typescript and javascript, features, components, installation, typescript types, etc.
🌐
HowToDoInJava
howtodoinjava.com › home › typescript › difference between undefined and null
JavaScript - Difference between undefined and null
September 22, 2021 - A variable is said to be “undefined” if it has been declared but not initialized. Whereas “null” is assigned to a variable whose value is absent.
🌐
TutorialsPoint
tutorialspoint.com › What-is-the-difference-between-null-and-undefined-in-JavaScript
What is the difference between null and undefined in JavaScript?
It means a variable declared, but no value has been assigned to the variable. JavaScript automatically assigns undefined to uninitialized variables. ... null is an intentional absence of a value. It is often used when a developer wants to indicate that a variable or object property has no value.
🌐
Hacker News
news.ycombinator.com › item
Ask HN: Should scripting engines distinguish between "null" and "undefined"? | Hacker News
March 13, 2024 - Do scripting languages really need both "null" and "undefined" values · i have long liked that JavaScript provides both "null" and "undefined", and have modeled my scripting languages after that since tinkering with them first became a hobby (right at 20 years ago).
🌐
Scaler
scaler.com › home › topics › javascript › null and undefined in javascript
Null and Undefined in JavaScript - Scaler Topics
April 4, 2024 - Besides, when a function doesn't return a value, it returns undefined. ... In JavaScript, to check the type of variable, we use the "typeof" operator. ... As discussed above, the type of null is an object.
🌐
DEV Community
dev.to › nunocpnp › differences-between-null-and-undefined-keywords-2e2m
Differences between "null" and "undefined" keywords? - DEV Community
September 5, 2019 - They both represent a empty value. Difference nr 1! When you define a variable but not as... Tagged with javascript, webdev, todayilearned, beginners.
🌐
2ality
2ality.com › 2021 › 01 › undefined-null-revisited.html
`undefined` vs. `null` revisited
Many programming languages have one “non-value” called null. It indicates that a variable does not currently point to an object – for example, when it hasn’t been initialized yet. In contrast, JavaScript has two such non-values: undefined and null. In this blog post, we examine how they differ and how to best use or avoid them.
Top answer
1 of 7
26

both are falsy values that denote absence of data, the only difference is apparently whether or not it was "intentional"

The main problem with Javascript having both null and undefined is that they are used inconsistently, even in standardised APIs. There is no absolute authority which says what each should be used to mean or indicate, and so different programmers imbue them with different meanings. Library code mostly has to treat them the same way, because there is no agreed standard for how they should be treated differently.

Perhaps to you, null means an "intentionally" missing value and undefined means an "unintentionally" missing value, but this isn't universal. The ECMAScript specification doesn't seem to have anything to say on the subject. MDN advises that null means "the intentional absence of any object value" but doesn't give the opposite advice for undefined. And as an example, canvas.getContext('2d') returns null when the same canvas is already in use in a 3D context, something very unlikely to be done intentionally.

Worse, "intentionally missing" isn't as clear a distinction as you might think. For example, function parameters take the value undefined when no argument is provided, but an argument could be missing either intentionally or unintentionally. Likewise, map.get(missingKey) returns undefined, but the key could be absent either intentionally or unintentionally. (Meanwhile, localStorage.getItem returns null instead in the same case.) The intent where a value is produced may also be different than where it is used.

As a result, if you come across some variable which might hold either an object or null or undefined, you will have to read the comments (hopefully there are some!) to figure out what the author intended by that. Perhaps the author didn't even intend to treat them differently, just there is a mix of undefined and null because those values came from different places and there was no need to unify them.

If instead there was a language-wide authoritative standard which said undefined means one thing and null means another, then this would be less of a problem, although there would still be code which doesn't follow the standard. That said, if you can think of a clear difference between what these two values should mean in your language, then you can probably think of better names for these values which would communicate those meanings.

2 of 7
7

It is useful but not necessary and there are better alternatives.

Dynamically typed languages have the limitations that they can only discover concrete types of values. That means they cannot differentiate between levels of a value being undefined: a function taking a value: Option[T] = None cannot find out whether it received the default None or was passed one if T is itself some Option[T'].

This can be practically fixed by having more than one "not defined" value: A function taking a value: T = undef can trivially separate the case of default undef and passed in None.
Of course this only shifts the problem: While for many practical problems eliminating half the collisions is enough, collisions still occur. A function that has to operate on both default and passed in undef/None still cannot separate the cases.

A more thorough approach is to allow/encourage defining new "not defined" values/types for each use. For example, Python has the idiom of using a hidden, bare object() as needed.

# placeholder with unique identity
_no_default = object()

def find(predicate, values, default = _no_default):
   ...

Of course a language can provide tools and syntactic sugar to make this simpler.

🌐
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?

🌐
Reddit
reddit.com › r/javascript › null vs undefined
r/javascript on Reddit: null vs undefined
March 26, 2017 - Just adding on: Notice how when you initialize a variable, it is always automatically set to undefined at first? It's because it literally does not have a definition. Things that are null do have a definition. They are defined as void. ... Oh man the null thing I didn't know about. Which has led to me go read this whole SO question: https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
🌐
Quora
quora.com › What-is-the-difference-between-undefined-and-null-in-JavaScript
What is the difference between undefined and null in JavaScript? - Quora
Referencing nothing, an inexistent instance is the null reference. Back to javascript: scopes are Dictionaries (Objects), and when the lookup of the key fails to locate something (parent scope, prototype chain etc, do check out reflection and js’s prototypes), or the something is there but uninitialized, we get undefined both as a type and value, since the type’s only possible value is undefined.