JavaScript check undefined | typeof and equality operator

If the variable declared without assigned any value then its initial value is undefined. You can use the typeof operator or if statement to check the value undefined in JavaScript.

Undefined variable

Example of JavaScript check undefined

HTML example code of comparing a variable using if statement. It works with modern browsers.

Use the equality operator (==)

Output:

Example of JavaScript check undefined

Using typeof

JavaScript check undefined typeof

Q: How to handle undefined in JavaScript

Answer: You can check the fact with.

Q: How to determine if the variable is checked if undefined or 0 in JavaScript?

Answer: To check if a variable is undefined or null you can use the equality operator == or strict equality operator === (also called identity operator).

Do comment if you have any doubts, questions or suggestions on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.