Javascript Program To Check If A Variable Is Undefined
In JavaScript, a variable without an assigned value is automatically undefined, differing from null, a specified data type. Understanding how to check for undefined versus null is vital for developers, as these concepts are fundamental yet often misunderstood in JavaScript programming.
Prerequisites
To understand this topic and the methods and to check undefined in javascript, the reader must be familiar with the following topic
- JavaScript Null
- Javascript Undefined
- JavaScript typeof() operator
- Functions and Functions expression in Javascript
Check Undefined or Null
In this method to check undefined in javascript, we will use the equality operator whether the variable is null or undefined, or neither of them.
Javascript Implementation
Output:
Explanation
- In the first step, we have declared 4 variables a, b, c, and d and initialized b with 21, c with 'string', and d with null
- Inside the check_value() function, we have applied the if-else statements to check whether the variable is null, undefined or none neither null nor undefined.
- Finally, we have printed the statement according to the if-else conditions
- We can see that the compiler is printing null for both undefined and null variable, this is because the equality operator == treat both null and undefined as null.
Using typeof
In this method to check undefined in javascript, we will use the typeof() operator to find the data type of an object. The typeof() operator will return the data type of the object which is passed as a parameter.
Javascript Implementation
Output:
Explanation
- In the first step, we created a class named Person and we declared the properties of the class i.e. name which is a string, age which is a number, and weight which is also a number.
- Inside the check_value() function, we have just returned the type of the variable using the typeof() function in Javascript.
- The function will return the string for name, the number for both age and weight, and undefined for address because the property of address is not defined in the class.
- Finally, we have printed all the types of the variable returned by the function.
Void Operator and Undefined
In this method to check undefined in javascript, we will use the void() operator to find whether the variable is undefined or not. The void(0) operator will always return the value undefined, so we can use this operator to compare it with the variables and find whether the variable is undefined or not.
Javascript Implementation
Output:
Explanation
- In the first step, we created a class named person and we declared the properties of the class i.e. name which is a string, age which is a number, and weight which is also a number.
- Inside the check_value() function, we have initialized the variable value1 with void(0) i.e. undefined and value2 with typeof(x), so that we can compare both of them
- The function will return the false for name, age, and weight because these variables are not undefined
- The function will return true in case of address because address property is not defined in the class and it will be equal to undefined.
- Finally, we have printed the value returned by the function i.e. either undefined or not undefined.
Checking Whether Object Properties are Undefined
In this method to check undefined in javascript, we will check whether the object properties are undefined or not using the typof() operator.
Javascript Implementation
Output:
- In the first step, we created a class named person and we declared the properties of the class i.e. name which is a string, age which is a number and weight which is also a number, address which is undefined initially.
- Inside the check_value() function, we have just returned the type of the variable using the typeof() function in Javascript.
- The function will return the string for name, the number for both age and weight, and undefined for both address and height because the property of address is defined as undefined and the property of height is not defined in the class.
- Finally, we have printed all the types of the variable returned by the function.
Conclusion
In this quick tutorial, we have discussed the methods to check undefined in Javascript, and we can conclude the article with the following.
- In JavaScript, a variable that is not assigned any value by the programmer initially is automatically assigned a value of undefined by the compiler.
- The undefined type is different from the null, as null is the specified data type but the undefined variable can not be specified.
- The typeof() operator will return the data type of the object which is passed as a parameter.
- The void(0) operator will always return the value 'undefined', so we can use this operator to compare it with the variables and find whether the variable is undefined or not.