InterviewVault
Welcome back, Sujit Kumar Mishra
Admin
SK Mishra
Revision Mode
Document technical questions and best-practice answers.
Can you explain the difference between undefined and undeclared variables in JavaScript, when it comes undefined and when it comes undeclared?
Undefined vs. Undeclared Variables in JavaScript (Easy Explanation):
Undefined:
A variable is undefined when you declare it, but don’t give it a value.
Example:
let x; console.log(x); // Output: undefined
Think: "I made a box, but didn’t put anything inside."
Undeclared:
A variable is undeclared when you try to use it, but never declared it at all.
Example:
console.log(y); // Error: y is not defined
Think: "I’m looking for a box that doesn’t exist."
Summary:
1: Undefined = declared, but not assigned a value.
2: Undeclared = never declared, using it causes an error.
Tip to Remember:
1: Undefined: Box exists, but empty.
2: Undeclared: Box doesn’t exist.