Zero vs NULL vs Undefined

Durgesh Parekh
4 min readApr 6, 2023

--

Hello Everyone,

I hope you are doing well.

You might be thinking, Why are you telling me this basic thing that we already know?

Image source: https://twitter.com/ddprrt

Today, I want to discuss the basic concepts of programming. You might get an idea from the title. If you are from a coding background, then you already know, but for non-coder, this blog might get boring. But I will explain this in a simple way so that you can get an idea about the concept. I know that these concepts are such that you sometimes get stuck and waste your time.

Yesterday, one of our team members who is new to development encountered a problem that required finding a solution within 5 minutes. However, it took around 15-20 minutes to solve it because they were stuck with an undefined error and could not find the answer. Please do not judge them for this as it was a learning experience for them.

Let’s get back to the point. Programming languages have different ways of representing the absence of a value or a variable. In JavaScript, we have three different ways to represent nothing: null, undefined, and zero. Each of these values represents a different kind of absence, and it’s important to understand the differences between them to write better code. We will also look at some code examples to illustrate their usage.

Zero:

Zero is a numerical value that represents the absence of a value. It is a valid value, and we can perform arithmetic operations with it. For example, if we have a variable x initialized to zero, we can add or subtract values to it.

let x = 0;
x = x + 5; // x is now 5
x = x - 2; // x is now 3

We can also use zero in conditional statements.

For example, if we have a function that returns a number, we can check if the value is zero using the strict equality operator.

function checkNumber(num) {
if (num === 0) {
console.log('The number is zero');
} else {
console.log('The number is not zero');
}
}
checkNumber(0); // The number is zero checkNumber(5); // The number is not zero.

Null:

Null is a value that represents the intentional absence of any object value. It means that a variable exists, but it has no value. Unlike zero, null is not a valid value for arithmetic operations.

In JavaScript, we can use the null value to indicate that a variable or object has no value explicitly. For example, if we have an object and one of its properties is not applicable, we can set it to null.

let person = {
name: 'John',
age: 25,
occupation: null
};

We can also use null to check whether an object property is missing.

if (person.occupation === null) {
console.log('Occupation is not applicable');
} else {
console.log('Occupation is ' + person.occupation);
}

Undefined:

Undefined is a value that represents the absence of a value. It is used to indicate that a variable has not been assigned a value.

For example, if we declare a variable without assigning a value, it will be undefined by default.

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

We can also have undefined values when accessing non-existing properties of an object.

let person = {
name: 'John',
age: 25
};
console.log(person.occupation); // undefined

Using Null vs Undefined vs Zero:

Now that we have seen the differences between null, undefined, and zero, we can discuss how to use them effectively in our code.

Null is used when we want to indicate that an object or variable has no value explicitly. It is a valid value, and we can use it to check for the absence of an object property.

Undefined is used to indicate that a variable has not been assigned a value. It is not a valid value, and we cannot perform arithmetic operations with it.

Zero is used to represent the absence of a value in a numerical context. It is a valid value, and we can perform arithmetic operations with it.

When working with variables and objects, initialising them with the correct value, either null, undefined, or zero, is essential, depending on the context. This will prevent errors and make our code more robust.

Conclusion:

In conclusion, null, undefined, and zero are prime values in JavaScript programming and understanding the difference between them is important.

Enjoyed the blog? Follow me for more similar content and stay connected.

Happy Coding!

Thank you.

--

--