

JavaScript — Null vs. Undefined – codeburst
source link: https://codeburst.io/javascript-null-vs-undefined-20f955215a2?gi=c28c99f98184
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JavaScript — Null vs. Undefined
Learn the differences and similarities between null and undefined in JavaScript

At first glance, null
and undefined
may seem the same, but they are far from it. This article will explore the differences and similarities between null
and undefined
in JavaScript.
What is null?
There are two features of null
you should understand:
null
is an empty or non-existent value.null
must be assigned.
Here’s an example. We assign the value of null
to a
:
let a = null;console.log(a);
// null
What is undefined?
Undefined most typically means a variable has been declared, but not defined. For example:
let b;console.log(b);
// undefined
You can also explicitly set a variable to equal undefined:
let c = undefined;console.log(c);
// undefined
Finally, when looking up non-existent properties in an object, you will receive undefined:
var d = {};console.log(d.fake);
// undefined
Similarities between null and undefined
In JavaScript there are only six falsy values. Both null
and undefined
are two of the six falsy values. Here’s a full list:
- false
- 0 (zero)
- “” (empty string)
- null
- undefined
- NaN (Not A Number)
Any other value in JavaScript is considered truthy.
If you’re not familiar with truthy/falsy values in JavaScript, I recommend reading my previous article: JavaScript — Double Equals vs. Triple Equals
Also in JavaScript, there are six primitive values. Both null
and undefined
are primitive values. Here is a full list:
- Boolean
- Null
- Undefined
- Number
- String
- Symbol
All other values in JavaScript are objects (objects, functions, arrays, etc.).
Interestingly enough, when using typeof
to test null
, it returns object
:
let a = null;
let b;console.log(typeof a);
// objectconsole.log(typeof b);
// undefined
This has occurred since the beginning of JavaScript and is generally regarded as a mistake in the original JavaScript implementation.
If you’re not familiar with data types in JavaScript, I recommend reading my previous article: JavaScript Data Types Explained
null !== undefined
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
.
null !== undefined
But, and this may surprise you, null
loosely equals undefined
.
null == undefined
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.
Practical Differences
All of this is great, but what about a practical difference between null
and undefined
?
Consider the following code snippet:
let logHi = (str = 'hi') => {
console.log(str);
}
The code above creates a function named logHi
. This function requires one parameter and sets the default of that parameter to hi
if it isn’t supplied. Here’s what that looks like:
logHi();
// hi
We can also supply a parameter to overwrite this default:
logHi('bye');
// bye
With default parameters, undefined
will use the default while null
does not.
logHi(undefined);
// hilogHi(null);
// null
Thanks to Tim Branyen for the code inspiration.
Summary
null
is an assigned value. It means nothing.undefined
typically means a variable has been declared but not defined yet.null
andundefined
are falsy values.null
andundefined
are both primitives. However an error shows thattypeof null = object
.null !== undefined
butnull == undefined
.
Closing Notes:
Thanks for reading! If you’re ready to finally learn Web Development, check out: The Ultimate Guide to Learning Full Stack Web Development in 6 months.
If you’re working towards becoming a better JavaScript Developer, check out: Ace Your Javascript Interview — Learn Algorithms + Data Structures.
I publish 4 articles on web development each week. Please consider entering your email here if you’d like to be added to my once-weekly email list, or follow me on Twitter.
If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇⬇
Recommend
-
79
The Future of JavaScript Will Be Less JavaScript
-
107
理解JavaScript中null、undefined和NaN
-
40
Welcome to Rant.js, a new series I’m starting based on short, mostly opinions, on anything that interests me in the JavaScript world. These articles, while not entirely factual, may provide an interesting look into the da...
-
43
One aspect of JavaScript development that many developers struggle with is dealing with optional values. What are the best strategies to minimize errors caused by values that could be null , undefined
-
13
原文网址:http://yanhaijing.com/javascript/2014/01/05/exploring-the-abyss-of-null-and-undefined-in-javas...
-
9
Javascript中的undefined、null、""、0值和false的区别总结 - JackPeng博客在程序语言中定义的各种各样的数据类型中,我们都会为其定义一个”空值”或”假值”,比如对象类型的空值null,.NET Framework中数据库字段的空值DBNull,boolean类型的假值false等等。在Ja...
-
5
Whenever I encounter null or undefined, I get incredibly confused by how each data type is used in JavaScript. I mean, what's the difference? Don't they both express the concept of nothing?
-
9
Javascript Check If an Object Is Null or Undefined While working in javascript, often we encounter a requirement that is to determine if the object is null or not. We might require this check to verify that th...
-
5
In JavaScript, null and undefined data types may appear similar when viewed at an abstract level. As a result, developers often need clarification and help when debugging errors related to these two values....
-
6
How to determine if variable is undefined or null in JavaScript 1482 views 2 years ago jQuery Use the equal...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK