Table of contents
No headings in the article.
Undefined | Undeclared | |
Definition | A variable that has been declared but has not been assigned a value. | A variable that has not been declared in any scope using the var, let or const keywords. |
Behavior | The variable exists in the current scope and its type is undefined. Accessing it does not cause an error; instead, it returns the special value undefined. | Attempting to access an undeclared variable will result in a RefrenceError. |
scope | Exists in the scope where it is declared. | Does not exist in any scope. |
Undefined:
let y;
console.log(y); // undefined
Undeclared:
console.log(x); // ReferenceError: x is not defined
Why we should know these concepts ?
Understanding these concepts helps avoid common pitfalls in JavaScript programming, such as runtime errors and unexpected behaviors.