Check for Undefined Value in JavaScriptWe will understand how to check for undefined value in JavaScript in this article. Let us comprehend what an undefined value is. UndefinedThe "undefined" is a global property that represents a primitive type value. It is utilized to represent the absence of any value. When a declared variable is not assigned any value then JavaScript shows an undefined value automatically. In the above syntax, we have declared a variable called "str" but we have not assigned it a value. When we output the variable "str" then it will output undefined. We can check undefined value by utilizing various methods which are given below:
Let us comprehend each method one by one. Utilizing equality operator (==)The equality operator is used to perform type coercion and transforms the operands to the identical type before doing the comparison. After conversion into the same type then it checks the undefined value so we can check whether the value is undefined or not by utilizing the equality operator (==). Demo: Output: We have checked the variable value utilizing the equality operator. We can witness in the output that the variable value is undefined. Utilizing identity operator (===)The identity operator does not perform type coercion like the equality operator but it checks both type and value equality so we can check whether the value is undefined or not by utilizing the identity operator (===). Demo: Output: We have checked the variable value utilizing the identity operator. We can witness the output in which the variable value is undefined. Utilizing typeof operatorThe "undefined" is a data type so we can utilize typeof operator to check whether the value is undefined or not. Demo: Output: We have checked the variable value utilizing the typeof operator. We can witness the output checks four variables. The first three variables are of string type and the fourth variable is undefined. Utilizing logical OR (||) operatorThe logical OR operator is utilized to give a default value to the variable when the variable is not declared. We can discover whether the value is undefined or not by utilizing the logical operator. Demo: Output: We have checked the variable value utilizing the logical operator. We have defined the variable "var1" so the output displays its value and we have not declared the value of variable "var2" so the output displays undefined value. Utilizing the void operator and undefinedThe void operator is utilized to check the whether the value is undefined primitive value or not. We have to utilize "void(0)" or "void 0" which always gives back the undefined value so it is used to compare with the variable to find out the undefined value. Code: Output: Here is the output in which we can witness that first three values are not undefined and the last value is undefined. Conclusion:We have understood how to check for undefined value in JavaScript. Some points to remember are as follows:
Next TopicCheck Substring in JavaScript |