Differences between "null" and "undefined" keywords?

nunocpnp profile image Nuno Pereira Updated on ・1 min read

They both represent a empty value.

Difference nr 1!

When you define a variable but not assign a value to it, it automatically puts a placeholder which is called undefined so you don't have do it manually, JavaScript does it for you.

Null means an empty or non-existent value.

Null is assigned, and explicitly means nothing. while undefined typically means a variable has been declared but not defined yet.

var a;
// undefined

var b = null;
// null

Difference nr 2!

Null and undefined are both primitives and falsy values. However null is also an object. Interestingly, this was actually an error in the original JavaScript implementation.

var a;
// undefined

var b = null;
// object

Difference nr 3!

As you can see so far, null and undefined are different, but share some similarities. Thus, it makes sense that null does not strictly equal undefined.

console.log(null !== undefined);
// true

But, and this may surprise you, null loosely equals undefined.

console.log(null == undefined);
// true

In JavaScript, a double equals tests for loose equality and preforms type coercion. This means we compare two values after converting them to a common type.

See you soon for more tips !


Editor guide
yujiri8 profile image
Ryan Westlund

I can think of two important things you didn't mention:

  • undefined doesn't come out in JSON, but null does

  • Function parameters are undefined if not passed - and not only that, but passing undefined to them doesn't override their default value:

function f(a = true) { console.log(a) }
f(undefined); // true
f(null); // null

So undefined can be useful with a multiparameter function to pass a value to a later parameter but not to an earlier one.

nunocpnp profile image
Nuno Pereira Author

Cool, thx for adding this up !

jamesthomson profile image
James Thomson

Null and undefined are both primitives and falsy values. However null is also an object.

And this is why no one will ever fully understand JavaScript 😆

Even taken from MDN:

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values.

And when you click through to primitive values, MDN states:

In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods.

Yet, typeof null // object 🤔

What the what 🤯

vlasales profile image
Vlastimil Pospichal
anna = {}
bill = {}

anna.age = null
bill.age = null
console.log(anna.age == bill.age)  // ???
console.log(anna.age === bill.age)  // ???

anna.age = NaN
bill.age = NaN
console.log(anna.age == bill.age)  // ???
console.log(anna.age === bill.age)  // ???
emptyother profile image

The new optional parameter for ES6 only cares about undefined.

function myFunc(a, b = 0) { console.log(a,b); }
myFunc(1); // 1 0
myFunc(1,null); // 1 null
myFunc(1,undefined); // 1 0
myFunc(1,getSomeNonExistingValue()); // 1 (null or 0)

Wouldn't it be simpler to use undefined instead of null everywhere? Just stop using null?

alexparra profile image
Alex Parra

Yeah, this has tripped me a couple of times...

_ezell_ profile image
Ezell Frazier

I like to think of undefined as the void personified.

umezvictor profile image
Victor Umezuruike

Sometimes it can be really confusing. Thanks for the explanation

nitinreddy3 profile image
Nitin Reddy

Yeah, JavaScript is a weird programming language but a nice one.

nunocpnp profile image