Undefined vs null

Hello. What is the difference between undefined and null?

Undefined means that a variable has been declared but not defined:

let a;
console.log(a); //prints undefined

Null is an assigned value. It’s a value of nothing.
let a = null;

Null is also a type of object, while undefined is a type of undefined.

(Note that you can also assign undefined to a variable.)
let a = undefined;

Hi, I like to think of undefined in JavaScript as missing (value). Hope that helps.

Thank you for your replies.

To check null in JavaScript, use triple equals operator(===) or Object.is() method. To find the difference between null and undefined, use the triple equality operator or Object.is() method.

To loosely check if the variable is null, use a double equality operator(==). The double equality operator can not tell the difference between null and undefined, so it counts as same. So if you want to strict check, then don’t use a double equality operator. It will lead to misconclusion.

Not the formal definition, but I like to see it this way:

Undefined means that something exists, but isn’t meaningful

Null means it doesn’t exist, or defined to be nothing.

I might be wrong on this though…

A lot of mythology in this thread :slight_smile:

To put it simply, Null is the “END”. Computers constantly deal with sequences of stuff (instructions in functions, values in objects or arrays etc.) and they need a way to be able to move along imaginary line between instructions in both directions. This raises a question, how computers will know where are the boundaries of each particular sequence , right? This question was solved by introduction of Null (or sometimes Nil) Pointer to a special address in memory that programs won’t be able to rewrite and therefore regardless of a program this rule will stand. In order for programs to be written every programming language must therefore have Null in their syntax and JavaScript isn’t an exception. Null in JavaScript points to the same point in memory as Null in Python, C++ or BashScript. Now back to present days, JavaScript doesn’t deal on those low levels and never allows programmer to set memory boundaries in any way, so usage of Null is “imaginary”, meaning that you can create tree of stuff and you will put Null to define boundaries of that tree (just like DOM) to mimic how things are done in memory :slight_smile:

JavaScript doesn’t need to “construct” Null it simply uses an existing pointer. Unlike Null, Undefined is a custom construct of JavaScript and therefore has to start somewhere. As all other constructs it starts at global object (window in browser) and therefore you can do silly stuff like this:

function CallMeAndGoodLuck() {
  Array = null;
  Number = null;
  undefined = null;
  Function = null;
  Object = null;
  // etc...
}

Just like all C-family languages (and probably others too), languages need Undefined to express uninitialized variables:

let a; // uninitialized variable
typeof a; // undefined

To put it simply, Undefined is “EMPTY”.

3 Likes

Regarding typeof null === 'object', hopefully what I’ve written above clearly shows that this was a bug initially and because of philosophy of JavaScript around backward compatibility it never had it’s chance to be fixed. By absolutely no means Null is an object and if you need a proof, try this:

// Every object-like structure in JavaScript is an instance of Object
new Date() instanceof Object; // true
[] instanceof Object; // true
(function fn() {}) instanceof Object; // true
(() => {}) instanceof Object; // true
new Boolean('false') instanceof Object; // true
null instanceof Object; // false
typeof null; // "object" 🐛

I’ve heard a story (I don’t know if it’s true) that in early days objects in JavaScript had memory allocated at addresses that started with “0x0…” and the way typeof was calculated is by checking if address starts with zero. Guess what has the address 0x0000 in computer memory? :wink:

1 Like

Thanks, really amazing answer :). I actually understand what is happening now!