Check for Undefined Value in JavaScript17 Mar 2025 | 3 min read We 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 TopicLambda-expression-in-javascript |
As the name implies, the split() method in JavaScript splits the string into the array of substrings, puts these substrings into an array, and returns the new array. It does not change the original string. When the string is empty, rather than returning an empty array,...
3 min read
Recursion in JavaScript is a programming method wherein a function calls itself at once or indirectly to resolve a problem. Recursion is an effective concept that helps function to be defined in terms of themselves which allows elegant solutions to certain styles of issues. There are some...
7 min read
In 2008, Google evolved the V8 engine to improve JavaScript velocity in Google Chrome, the business enterprise's online browser. Large-scale tasks had been formerly concept to require JavaScript to be slow and inefficient, especially when in comparison to other programming languages like Java or C. Before...
10 min read
What is ? In JavaScript, a new line is generally represented via a specific get away collection or person within a string. It is normally utilized in numerous programming languages to control the formatting of textual content and create more than one strings. In easy phrases, the...
4 min read
It is considered a best practice in programming to create distinct functions or methods for handling specific actions. While it is feasible to consolidate all actions into a single method or function, adhering to best practices can negatively impact the readability and clarity of the code....
12 min read
The getAttribute() method is used to get the value of an attribute of the particular element. If the attribute exists, it returns the string representing the value of the corresponding attribute. If the corresponding attribute does not exist, it will return an empty string or...
2 min read
What is Moment.Js? An open-supply JavaScript bundle known as Moment.Js makes it less difficult to control, validate, and format dates and timings. Since its introduction through Tim Wood in 2011, this pass-to solution for date and time operations has grown in recognition amongst builders. The library is...
10 min read
Introduction Data structures and algorithms structure the foundation of software engineering and programming. They are fundamental apparatuses for taking care of complicated issues productively and are urgent in building vigorous and versatile applications. In this masterclass, we will dive into the universe of JavaScript algorithms and data...
19 min read
The is used to return a string that represents the type of JavaScript for a given value. It returns the data type of the operand in the form of a string. The operand can be a literal or a data structure like a function,...
3 min read
JavaScript applications that can be saved as a bookmark's URL in a web browser are referred to as bookmarklets or JavaScript bookmarks. JavaScript bookmarks run code within the current internet web page, as antagonistic to conventional bookmarks, which most effectively store a webpage's URL for convenient...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India