Binge On Code

Jun 10, 2023

21 readers

JavaScript Null And Undefined Explained Using Car Parking Example

Yeah, JavaScript Null And Undefined are oftentimes used interchangeably. So, how about we demystify these two, by using a typical car parking lot!

To hit the nail on the head when it comes to JavaScript null and undefined, JavaScript null refers to an existing parking lot while JavaScript undefined refers to a non-existent one.

This means that null alludes to the presence of a space that can or can not have a car on it, while undefined means that the parking space itself is not there.

That is basically it, we can call it wraps here. But if you want to, keep on reading.

Null

Think of null as a physical parking space which can exist in two different states:

  1. An empty parking space

  2. A parking space with a car

A JavaScript null means that that specific reference can actually have a value or not. 

Considering example #1 above, the parking space can at times be empty, just because it is empty does not mean it is non existent.

Now, when someone decides to park on a given space, that is where we have example #2, the space now has a value as needed.

Undefined

Well, as for JavaScript undefined, this basically means that the parking space never existed in the first place. It is not there at all, and unless it is created it cannot be capable of accommodating a car.

In Summary

Null shows / gives the possibility of a value existing in the reserved space, on the other hand, undefined eliminates the presence of space.

Related Articles

Code Splitting And Lazy Loading

Code Splitting and lazy loading are basically augmentations of one another. Lazy loading builds on top of the power of code splitting.

Jun 8, 2023

Views 498

Destructure JavaScript Object With Defaults

You can really simplify your access to your JavaScript objects by using this simple method to destructure JavaScript object with defaults.

Jun 20, 2023

Views 27

Get Index Of Item In JavaScript For Of Loop

In a few lines of code, you will see how easy it is to get an item’s index in JavaScript For Of Loop. Let’s do this!

Jun 16, 2023

Views 34

Angular JavaScript React CSS Django Python React Native Next JS State Management Django Rest Framework Unity