Undefined vs Undeclared [Javascript]

Undefined vs Undeclared [Javascript]

Table of contents

No heading

No headings in the article.

UndefinedUndeclared
DefinitionA 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.
BehaviorThe 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.
scopeExists 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.