JavaScript Null vs. Undefined Explained

null vs. undefined

JavaScript is an interesting language with lots of quirks. One such confusing aspect in JavaScript is the differences and similarities between Null and Undefined. Most programmers are aware of null. It is common in other programming languages. But what on earth is undefined in JavaScript?

null in JavaScript

null means there is no value. Just like in other programming languages, you can assign a value of null to a variable, which indicates that it has no value. In the example below, we are assigning a to null.

let value = null;
// null 

undefined in JavaScript

Alright, we get what null does. What does undefined really mean?

Undefined usually means a variable has been declared, but not defined.

Let’s tweak our previous example a little bit to understand undefined.

let value;
// undefined

In this case value has been declared but it is not assigned a value. This results in undefined. Just like null, you can also set a variable to a value of undefined.

let value = undefined;
// undefined

Another common scenario, where you will encounter an undefined is while trying to access a property of an object that doesn’t exist. Let’s see an example below illustrating that.

let myObject = {};
// undefined

To summarize, here are some of the scenarios where you will see undefined being returned.

  • An object is declared but not defined/initialized.
  • Accessing an object property or an array index that does not exist.
  • Calling a function, without it’s required function parameters.

What is similar?

Both null and undefined are primitive values in JavaScript. Some other primitive values include Boolean, String and Number. Another thing to note is that when comparing null and undefined they are equal. This is because both of them are considered falsy values in JavaScript.

null == undefined

Differences null vs. undefined


I have seen many codebases with a lot of null checking against properties that are uninitialized. This is wrong and will result in unexpected behavior. Uninitialized properties in other languages get a default value of null, but in JavaScript they are undefined. What makes it even more misleading, is that if you did a loose equality check of (x == null), even if x is undefined, it will return true. This is because of the type coercion that happens in JavaScript.

null is not strictly equal to undefined. But null is loosely equal to undefined.

console.log(typeof(undefined));  //"undefined"
console.log(typeof(null));       //"object"

Notice here that undefined is of type undefined whereas null is an object.

null !== undefined
null == undefined

Isn’t JavaScript a funny language with all these quirks? You can learn more about Common Gotchas in JavaScript from our previous article.

Arithmetic Operations

Another interesting difference to note is when arithmetic operations are performed with null vs. undefined. Arithmetic operations with null value will result in integer value while any arithmetic operation with undefined will result in value of variable being changed to NaN.

let a = 5 + null;
// 5

let b = 5 + undefined;
// NaN 


JavaScript is easy to pick up, but hard to master. It takes a while to learn the quirkiness and nuances of the language before you master it. If you are looking for an awesome course to master you JavaScript proficiency, I highly recommend Mosh’s courses on JavaScript. Checkout the bundle below for Mosh’s JS courses.

Mosh’s JavaScript Courses Bundle

I hope you enjoyed this post. If you did please share and spread the word. You can follow me on twitter at @AdhithiRavi

Checkout this space for more fun articles!

Adhithi Ravichandran is a Software Consultant based in Kansas City. She is currently working on building apps with React, React Native and GraphQL. She is a Conference Speaker, Pluralsight Author, Blogger and Software Consultant. She is passionate about teaching and thrives to contribute to the tech community with her courses, blog posts and speaking engagements.
Tags: , , ,

Leave a Reply

Connect with Me
  • Categories
  • Popular Posts