codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

JavaScript — What’s the difference between Null & Undefined?

Brandon Morelli
codeburst
Published in
2 min readJun 15, 2017

--

Null and Undefined have a few subtle differences. Learn the details in two minutes.

A whole lot of ‘null’

Preface

Hey! I’m Brandon. I created codeburst.io and I write JavaScript tutorials and articles to help beginners better understand the inner workings of Web Development. If you have any questions about the article, leave a comment and I’ll get back to you, or find me on twitter @brandonmorelli. Lastly, when you’re ready to really dive into Web Development, Check out the Best Courses for Learning Full Stack Web Development

null !== undefined

Null

Null means an empty or non-existent value. Null is assigned, and explicitly means nothing.

var test1 = null;console.log(test1);
// null

null is also an object. Interestingly, this was actually an error in the original JavaScript implementation:

console.log(typeof test1);
// object

Undefined

Undefined means a variable has been declared, but the value of that variable has not yet been defined. For example:

var test2;console.log(test2);
// undefined

Unlike null, undefined is of the type undefined:

console.log(typeof test2);
// undefined

Here are a two other examples of how you would get an undefined variable in JavaScript:

  • Declare a variable then assign undefined to it:
var test = undefined;console.log(test);
// undefined
  • Looking up non-existent properties in an object:
var test = {};console.log(test.prop);
// undefined

Summarize it for me, Brandon

Here are the quick facts:

  • null is an assigned value. It means nothing.
  • undefined means a variable has been declared but not defined yet.
  • null is an object. undefined is of type undefined.
  • null !== undefined but null == undefined.

You did it.

Good work! You can now differentiate between null and undefined. I publish a few articles and tutorials each week, please enter your email here if you’d like to be added to my once-weekly email list.

❤ If this post was helpful, please hit the little blue heart! And don’t forget to check out my other recent articles:

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Brandon Morelli

Creator of @codeburstio — Frequently posting web development tutorials & articles. Follow me on Twitter too: @BrandonMorelli

Responses (13)

Write a response