DEV Community

Cover image for 🚫 Undefined vs Not Defined in JavaScript: What's the Difference?
ronak wanjari
ronak wanjari

Posted on

🚫 Undefined vs Not Defined in JavaScript: What's the Difference?

By Ronak Wanjari | Intern at Devsync.in

JavaScript is powerful β€” but sometimes, its behavior can feel a little confusing. One common pain point for developers, especially beginners, is understanding the difference between undefined and not defined. Although they might seem similar at first, they represent very different things under the hood.

πŸ‘¨β€πŸ’» I’m currently learning JavaScript as part of my internship at @devsyncin, where we focus on building modern, scalable web applications. This blog reflects a hands-on concept I recently learned and found incredibly useful in avoiding bugs and writing clean code.

🚦 Understanding undefined
When you declare a variable but don’t assign a value to it, JavaScript automatically gives it a value of undefined.

let a;
console.log(a); 
Enter fullscreen mode Exit fullscreen mode

In this case, the variable a exists, but it hasn't been given any value yet β€” hence, it’s undefined. This isn't an error; it's JavaScript’s default behavior.

❌ What Does "Not Defined" Mean?

Now let’s look at a different scenario:

console.log(b); // ReferenceError: b is not defined
Enter fullscreen mode Exit fullscreen mode

Here, b was never declared β€” not with let, const, or var. So, when JavaScript encounters console.log(b), it throws a ReferenceError, saying that b is not defined.

This error stops your program immediately. It usually means one of two things:
*You forgot to declare the variable.
*You made a typo in the variable name.

🧩 Summary

βœ… undefined: The variable exists but has no value.

❌ not defined: The variable doesn’t exist at all β€” and trying to use it will crash your code.

πŸ” Use typeof to safely check for variable existence.

Thanks to my ongoing internship at @devsyncin , I’ve been able to learn these core JavaScript concepts through real-world tasks and challenges. Understanding the difference between undefined and not defined has not only helped me debug better but also write cleaner, more predictable code.

#javascript #webdevelopment #programming #devsync #learnjavascript #beginners

Top comments (0)