Difference Between Null and Undefined

Gema
1 min readAug 10, 2022

If you just start programming, you might be wondering what is undefined, like why my function returning undefined or returning nothing. Here I will try to explain to you what’s undefined and what’s null.

Undefined is a variable that refers to something that doesn’t exist, and variable isn’t defined to be anything. Here is an example:

let firstName;
console.log(firstName) // undefined

This console will return undefined, why? you don’t pass any value to the variable. From here, you might have a clearer picture now, like let’s say you have a bunch of functions and one of your functions returning undefined, this could mean that your function is returning an empty value.

How about null? null is a variable that is defined but is missing a value.

let firstName = null;
console.log(firstName) // null

This console will return null, pretty straightforward, right? but can we say that null is equal to undefined, the answer is “not really”. If you compare null and undefined using the Loose Equality Operator (==) the answer will be true, but if you compare it using the Strict Equality Operator (===) the answer will be false. So be mindful if you might use this kind of operation in your code.

null === undefined   // false
null == undefined // true

That’s it folks, hope this article helps you in your journey to become a software engineer (xd), thanks and see you again!

--

--

Gema

Martech | Solutions Engineer | Gamer | Weebs | Football Enthusiast → contact: gemamreza@gmail.com