JavaScript Course
JavaScript
/
Beginner

Variables (var, let, const)

Definition

Variables are containers for storing data values. In JavaScript, you can declare them using var, let, or const. They determine how the stored data can be accessed and if it can be changed.

Explain Like I'm New

Think of variables like labeled boxes. You put something inside the box, and you can find it later by looking for the label. 'let' is a box whose contents you can change. 'const' is a locked box where the contents cannot be swapped out. 'var' is an older type of box that behaves a bit unpredictably.

Real World Example

When playing a video game, your 'score' is a variable that changes (like 'let'), while your 'maxHealth' might be a fixed value that never changes (like 'const').

Common Use Cases

  • •Storing user input
  • •Keeping track of application state (e.g., is user logged in?)
  • •Holding configuration values that never change

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What is the difference between let, const, and var?
  • Can you reassign a const variable?

intermediate

  • What does 'block scope' mean for let and const?
  • Can you mutate an object or array declared with const?

advanced

  • What is the Temporal Dead Zone (TDZ)?
  • How does the JavaScript engine handle variable declarations during the creation phase?

trick

  • If you declare a variable without a keyword inside a function, what happens?

Flash Cards

Question

What is the difference between let, const, and var?

Click to reveal answer
Answer

'var' is function-scoped and can be re-declared. 'let' is block-scoped and can be updated but not re-declared in the same scope. 'const' is block-scoped and cannot be updated or re-declared.

Question

Can you mutate an object declared with const?

Click to reveal answer
Answer

Yes. 'const' only prevents reassignment of the variable identifier. The properties of the object itself can still be changed (mutated).

Question

What is the Temporal Dead Zone?

Click to reveal answer
Answer

The time between when a 'let' or 'const' variable is hoisted (moved to the top of the scope) and when it is actually initialized with a value. Accessing it during this time throws a ReferenceError.