null vs undefined

David Shin
2 min readSep 13, 2020

This could quite confuse where you step to move to the next level as you proceed to learn Javascript, null, and undefined. As it sounds first in my mind when I saw these two words, I was pretty sure both do negative value and probably the same type. However, in Javascript it is totally different, let take a look.

So what is null? and undefined? First, let’s discuss with primitive data before we conclude the difference between those two words. According to the MDN website, there are 6 primitive data types which are string, number, bigint, boolean, undefined, and symbol. Yet there is still one special case that is classified as primitive data is called null. So both null and undefined are represented directly at the lowest level of the language implementation, which means most of the time they both are not an object and have no method.

Know we know that both are primitive values, and let’s know defined one by one. Undefined (accordingly from MDN) is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments. For example, let’s say we are declaring a new variable called foo, right that moment we declared whether we are assigned with a value or not, as its first step , the variable automatically will get assigned with an undefined value. (ex. var foo; will print => undefined) In case if there is not value that assigns to that variable it still exits but simply undefined yet and with that being said it’s not an empty value!

What if we want to assign that value to an empty address or an object? Now we know where this is heading, yes null is the perfect match for this solution. If we assign to a new variable with null that means the assigned variable with null, it’s not undefined, but simply empty until it assigned with something else. Although both do share a common topic which null and undefined both determined as falsy value. Don’t get confused that both are not equal one to each other, meaning null === undefined is big no-no-no! There are not the same type or value.

We may say this null and undefined a lot in various codes, and both types of data are used in numerous situation with handy. So let’s make sure that we are using in the right direction.

Resources:

--

--