How to check for undefined in JavaScript?

How to check for undefined in JavaScript?

In JavaScript, variables can be primitive or non-primitive in nature. Primitive data types in JavaScript include numbers, booleans, strings, symbols, etc., whereas objects and arrays are considered non-primitive data types. However, there are two additional primitive types in JavaScript which are undefined and null. These two data types may look similar, but there are a few key differences. In order to understand the undefined keyword properly, we need to be aware of its differences from the null type.

So in this article, we will look at what is the undefined data type, the difference between undefined and null, and how you can check for undefine values in javascript properly.

What is undefined?

undefined is one of the primitive data types in JavaScript that occurs when a variable is not assigned a value yet. It is the default value of a variable when it is declared, but there is no value assigned to the variable. We can better understand this with the help of a simple example.

let testVar;

console.log(testVar); // undefinedCode language: JavaScript (javascript)

Here the value of the variable testVar is undefined since we haven’t assigned any value to it.

We can also explicitly set a variable to be undefined. Let us look at an example,

let testVar = 20;
testVar = undefined;

console.log(testVar);  // undefinedCode language: JavaScript (javascript)

In this case, we initially set the value of the variable testVar to 20, but then we assigned its value to be undefined. So ultimately, the value testVar turned out to be undefined.

Difference between undefined and null

Now that we understand what is undefined, let us look at some key differences between undefined and null since they are often misunderstood as being the same.

  • In JavaScript, variables that have not been defined or have been assigned an empty value are treated as undefined.
  • On the other hand, the null value represents an object that is not available.
  • Null is usually used to represent missing values when dealing with variables related to a database or API.

Let’s look at a small table to solidify our understanding of the differences between the two,

undefinednull
undefined is the value assigned to a variable when it’s declared but not assigned a value.null is explicitly assigned to variables to indicate the absence of a value.
The typeof undefined returns undefined.The typeof null returns object.
Using a undefined value in an arithmetic operation will result in NaN.When using null in arithmetic operations, it will be converted to 0 implicitly.

This can get a little confusing, but the best way to distinguish the difference is that null is used to represent something that is missing, while undefined is used to represent a variable that has not been assigned a value.

How to check for undefined in JavaScript?

There are a few ways to check if a variable is undefined in javascript; let us look at them with examples.

Direct Comparison

The simplest way to check for undefined in JavaScript is simply to use an if/else statement and check if the value is equal to undefined using the === operator. Let us look at a code example for the same,

let testVar;

if (testVar === undefined) {
    console.log("The value is undefined");
}
// Output- "The value is undefined"Code language: JavaScript (javascript)

Using the typeof keyword

The typeof keyword is used to check the type of a variable. In this case, we can use it to check if the variable type is “undefined”. Please note that when using the typeof keyword, we need to compare it with the string version of the data type, i.e., “undefined”. Let us look at an example to understand it better,

let testVar;

if (typeof testVar === "undefined") {
    console.log("The value is undefined");
}
// Output- "The value is undefined"Code language: JavaScript (javascript)

Using void operator

We can also use the void operator to obtain the undefined value or check for it within an if/else statement. You can do so by replacing undefined with void(0) as shown below,

let testVar;

if (testVar === void(0)) {
    console.log("The value is undefined");
}
// Output- "The value is undefined"Code language: JavaScript (javascript)

Conclusion

In this article, we’ve seen what is undefined, understood the difference between undefined and null, and learned how we could check for undefined in JavaScript. Undefined values in JavaScript can often be problematic and confusing for developers. If you notice that a variable is undefined, you can assign it a value to fix the issue.

If you have any questions or want to talk about anything technology, you can find me on Twitter. Thank you for reading!

Sharing is caring

Did you like what Supantha Paul wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far