How to Check undefined in JavaScript?

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

In Javascript, the better way to check if the variable is undefined or not using typeof operator.
In Javascript, the undefined is a property which indicates that a variable has not been assigned a value, or not declared at all.

Check undefined in JavaScript?

Example 1: Test if a variable is undefined:

var x;
if (typeof x === "undefined") {
    console.log("x is undefined");
} else {
    console.log("x is defined");
}
Output:
x is undefined

Example 2: Test if a not declared variable is undefined:

if (typeof x === "undefined") {
    console.log("x is undefined");
} else {
    console.log("x is defined");
}
Output:
x is undefined
Note that, we have not declared x variable in the above example.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare