Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to check undefined in JavaScript

Understanding Undefined in JavaScript

In your journey through the world of JavaScript, you might have come across the term 'undefined'. But what is it really? Let's dive into this concept and understand it in a simple and lucid manner.

What is Undefined?

Undefined in JavaScript signifies that a variable has not been assigned a value. Imagine you are a librarian and a book is 'undefined' if it is not yet cataloged. Until you give it a catalog number, it remains 'undefined'.

let myVariable;
console.log(myVariable);

This will output undefined because no value has been assigned to myVariable.

Checking for Undefined

A common task while programming is to check if a variable has been defined before trying to use it. This is similar to checking if a book has been cataloged before lending it out.

In JavaScript, we can check for undefined in multiple ways.

Using typeof Operator

The typeof operator is a unary operator that returns a string indicating the type of the unevaluated operand. Unary operator, in simpler terms, is an operator that takes only one operand or argument.

let myVariable;
console.log(typeof myVariable);

This will output "undefined" as the variable myVariable has not been assigned any value.

Using Strict Equality Operator (===)

The strict equality operator (===) checks whether two values are exactly the same, including their type.

let myVariable;
console.log(myVariable === undefined);

This will output true as the variable myVariable has not been assigned any value.

Undefined vs Null

Often, beginners can confuse undefined with null. However, they are different. Null is a value that represents no value or no object. It implies an empty or non-existent value. The librarian analogy would be a book that has been cataloged, but the catalog entry says 'no book'.

let myVariable = null;
console.log(myVariable);

This will output null.

To check if a variable is null, we use the strict equality operator (===) again.

let myVariable = null;
console.log(myVariable === null);

This will output true.

Best Practices

It's important to follow certain best practices when dealing with undefined in JavaScript.

Do not assign undefined to variables. Let JavaScript engine do it for you when you do not assign a value to a variable.

Use typeof to check if a variable is undefined. This is the safest way as it does not throw an error even if the variable has not been declared.

Use strict equality operator (===) to check for null or undefined. Loose equality operator (==) can lead to unexpected results due to type coercion.

Conclusion

Understanding 'undefined' in JavaScript is like learning to navigate the catalog system of a library. It might seem daunting at first, but with practice comes ease. So, the next time you come across an 'undefined', don't panic. Remember, it's just JavaScript's way of saying, "Hey, you haven't assigned a value to this variable yet!"

Your journey as a JavaScript developer will be filled with many more such concepts. And, just like understanding 'undefined', you will conquer them all, one step at a time. Happy coding!