How to check if variable is undefined or null in JavaScript?

Today, we will show you the best ways to check for undefined or null variables or object in JavaScript. 

By using these solutions, you can improve your JavaScript applications and avoid potential errors.

In JavaScript programming, it’s really important to handle undefined and null values correctly to make sure your code works smoothly and reliably.



Let’s dive into the world of handling how to check null or undefined in JavaScript.

What is undefined or null means in JavaScript?

In JavaScript, when a variable is declared but not given a value, it is considered undefined.

On the other hand, null is a special value that represents the absence of a value or object.

You can assign null to a variable to indicate that it has no value.

For instance, if you declare a variable like:

 var i; 

And try to print its value using:

console.log(i);

Output:

undefined

It will display undefined because the variable has been declared, but not assigned any value.

However, if you assign null to the variable like:

 t = null;

And then print its value using:

console.log(t);

Output:

null

It will display null because the variable has been defined, but there’s a missing value.

In a simple understanding, undefined indicates that a variable has been declared but lacks a value or has not been assigned any value, while null is a value that denotes the absence of a value or object.

Different ways to check if variable is undefined or null in JavaScript

To check if a variable is undefined or null, you can use different methods in JavaScript.
Here are some common ways:

1. Use the equality operator (==)

You can use the equality operator (==) in JavaScript to check if a variable is undefined or null.

This operator compares values and converts them to the same type before making the comparison. Since both undefined and null are considered false values.

You can use the following code to check if a variable is undefined or null:

if (variable == null) {
  // variable is undefined or null
}

Here’s an example code:

let i;

if (i == null) {
  console.log("variable is undefined or null");
} else {
  console.log("variable is not undefined or null");
}

Output:

variable is undefined or null

2. Use the strict equality operator (===)

To check if a variable is undefined or null in JavaScript, you can use the strict equality operator (===).

Unlike the regular equality operator, it doesn’t convert the types of the values being compared.

It only returns true if the variable is of the same type and has the exact value of undefined or null.

Here’s the following code to check if a variable is undefined or null:

if (variable === undefined || variable === null) {
  // variable is undefined or null
}

Here’s an example code:

let t;

if (t === undefined || variable === null) {
  console.log("variable is undefined or null");
} else {
  console.log("variable is not undefined or null");
}

Output:

variable is undefined or null

3. Use the typeof operator

Another way to check if a variable is undefined, you can use the typeof operator in JavaScript.

It gives you the type of the variable as a string. If the variable is undefined, typeof will return “undefined”.

Here’s the following code to check if a variable is undefined or null:

if (typeof variable === "undefined") {
  // variable is undefined
}

Here’s an example code:

let t;

if (typeof t === "undefined") {
  console.log("variable is undefined");
} else if (t === null) {
  console.log("variable is null");
} else {
  console.log("variable is not undefined or null");
}

Output:

variable is undefined

Moreover, you can use the typeof operator to check if a variable is defined or undefined in JavaScript.

When you apply typeof to a variable, it will give you “undefined” if the variable is not initialized, and “null” if the variable intentionally has no value assigned to it.

Conclusion

To sum up, this article explores different approaches on how to check if a variable is undefined or null in JavaScript.

This article discussed the concepts of undefined and null in JavaScript, explaining that undefined represents a variable without a value or assignment, while null indicates the absence of a value or object.

We are hoping that this article provides you with enough information that helps you understand the JavaScript check undefined or null. 

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment