As we know, variables in JS are the named memory locations. We use different ways to define a variable and sometimes we don’t assign any value to the variable, which is called undefined variables. In this article, we will look into various methods to check if a Variable is Undefined in JavaScript or not. Let us get started!
What are undefined Variables?
Undefined, in general terms, means “not-defined”. An undefined variable in JavaScript refers to a variable that has been declared but has not been assigned any value. When a variable is not assigned any value, it holds a garbage value. Undefined variables can also occur when we try to access the invalid index of an array.
Let us see a few examples of the same. Look at the code below:
//declaring a variable let num; //printing the variable num console.log(num);
Output:
undefined
In this example, we have declared a variable “num”, but we have not assigned any value to the variable, so “num” does not hold a defined value. Hence, the output is undefined. We have obtained the desired output.
Let us see another example where an undefined value can occur:
//declaring an array let arr=[1,2,3,4,5,6]; //printing the 6th index value of array console.log(arr[6]);
Output:
undefined
In this example, we are trying to access the value at a non-existent index of an array. The array’s length is 6 and the indexing starts from zero(0). So, the index of the last element of the array is Five (5). Therefore, accessing any index greater than five will give us an undefined value. Hence, we have got the desired output.
Let us take another example of an object:
//declaring an object let obj={ name: "Rachel", age: 17 } //accessing non existent key in the object console.log(obj.income);
Output
undefined
In this example, we have an object with two key-value pairs. The values can be accessed with the help of the keys. The keys are name and age. There’s no key “income”. Hence, when we try to access the value corresponding to a non-existent key (like income), the outcome is an undefined value. Hence, we have obtained the desired result.
Note that it is different than null. In JavaScript, “undefined” indicates that a variable has been declared without assigning a value or that a property does not exist in an object. On the other hand, “null” represents the intentional absence of any object value.
3 Ways to Check for Undefined in JavaScript
In this section, we will look into various methods to check if a variable has an undefined value:
1.) Direct Comparison
One of the most simple methods to check if a variable is undefined is through direct comparison. We compare the value of the variable to the undefined keyword. By this, we determine if the variable is undefined or not.
Let us see an example to do it practically:
//declaring a variable let num; //checking for the undefined value if(num==undefined){ console.log("The variable holds an undefined value."); } else{ console.log("The variable does not hold an undefined value."); }
Output:
The variable holds an undefined value.
We directly compare the value that the variable holds with the undefined keyword. The “if” condition is true and hence we have obtained the desired result.
2) Using the typeof Operator
The typeof operator is used to check the type of any variable. So, using typeof will also help us identify if the variable holds an undefined value or not.
//declaring a variable let num; //using the typeof operator console.log(typeof(num));
Output:
undefined
We have used the typeof operator to check if the variable is defined or not. It returns the type as undefined. Hence, we have obtained the desired result.
3) Using the Void Operator
The void operator is another method to check if a variable is undefined. The void operator evaluates an expression and returns undefined as its result. Check the code below:
//declaring a variable let num; //using the void operator if(num==void 0){ console.log("The variable num is undefined."); } else{ console.log("The variable num holds a defined value."); }
Output
The variable num is undefined.
We compare the variable with the void 0 operator. If they are the same, then the variable is undefined. Hence, we have obtained the desired result.
Handling undefined Variables in Functions
Functions may or may not use parameters. When functions have parameters, we have to make sure to check for the undefined value. Let us see an example:
//declaring a function function sayHello(planetName){ //direct comparison with keyword if(planetName==undefined){ console.log("Hello World!"); } else{ console.log("Hello "+planetName+"!"); } } //declaring planet variable let planet; //calling the function sayHello(planet); //assigning a value to the planet variable planet="Earth"; //calling the function again sayHello(planet);
Output:
Hello World!
Hello Earth!
In this, we handle the undefined parameter in the function by using an “if” condition. If the parameter is undefined, “Hello World” gets printed. If the parameter is not undefined, we print the statement as “Hello [planetName]”. We first use the undefined parameter and print “Hello World!”. Then we assign the value “Earth” to the planet variable and print “Hello Earth!”.
We have obtained the desired result.
Conclusion
In JavaScript, It is essential to understand how to check for undefined variables to write robust and clean code. In this article, we first discussed various cases where it can occur. We can use the above-discussed methods such as direct comparison, typeof Operator, or Void Operator to check it. If you have any more doubts, get our expert-level JavaScript homework help instantly.