Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is null in JavaScript

Journey to the Null Island

Welcome aboard, future developers! Today, we're going to take a trip to a peculiar place in the world of JavaScript. It's called "Null Island". Sounds interesting, doesn't it? Let's start exploring!

What is Null?

In JavaScript, null is not a jargon or a complex concept, but a simple, special value. Think of it as an empty place on a shelf or an empty box. It's there, taking up space, but there's nothing inside it.

In the technical sense, null represents the intentional absence of any object value. In simpler words, it means there is no value or no object.

Here's how you can declare a variable with a null value:

let variable = null;

In this case, variable doesn't have a value or an object. It's empty, or null.

Null is not Undefined

One common pitfall for new developers is to confuse null with undefined. While both seem to indicate emptiness or absence of value, they are not the same.

Consider undefined as an empty plot of land where nothing has been built yet. On the other hand, null is like a plot where a building was planned but never built. The foundation was laid, but there's no building there.

In JavaScript, when a variable is declared but not assigned a value, it's undefined.

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

But when a variable is declared and explicitly assigned null, it's null.

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

Null in Real-World Scenarios

You might be wondering, "When would I use null?". Let's take a look at a real-world scenario.

Imagine you're creating a web application where users can create a profile. When a new user signs up, they might not provide all their information immediately. In your JavaScript code, you would initialize all the profile fields as null.

let userProfile = {
  name: null,
  age: null,
  email: null
};

Later, when the user fills out their profile, you can replace null with actual values.

The typeof Null Quirk

JavaScript has a quirky behavior when it comes to checking the type of null. When you use the typeof operator on null, it returns "object". This might seem strange since we said null represents the absence of an object.

console.log(typeof null); // object

Despite this oddity, always remember that null is not an object; it's a special value representing the absence of an object.

How to Check for Null

To check if a variable is null, use the strict equality (===) or inequality (!==) operator.

let variable = null;

if (variable === null) {
  console.log("variable is null");
}

Conclusion

Imagine you're a ship captain sailing the vast seas of JavaScript. There are numerous islands (values) out there. But null is that special island, where nothing exists. It's not that the island is undefined or doesn't exist on the map, it's just that there's nothing there.

Understanding null is a crucial step in your journey as a JavaScript developer. It's a concept that you'll encounter frequently. Remember, null is not an ominous void, but a friendly signpost, indicating that you've intentionally set a variable to have no value... yet. So, the next time you come across null, don't be alarmed. It's just your code telling you that it doesn't have a value for a certain variable at the moment. And that's perfectly okay!

Happy coding, and may you always find your way back from Null Island!