larryryan1986
larryryan1986 5d ago โ€ข 10 views

JavaScript Variables Quiz: Test Your Knowledge of var, let, const

Hey everyone! ๐Ÿ‘‹ Ready to supercharge your JavaScript skills? Understanding `var`, `let`, and `const` is fundamental, and it can sometimes be a bit tricky to keep straight. This quick study guide and quiz will help you solidify your knowledge and avoid common pitfalls. Let's dive in! ๐Ÿš€
๐Ÿ’ป Computer Science & Technology
๐Ÿช„

๐Ÿš€ Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

โœจ Generate Custom Content

1 Answers

โœ… Best Answer
User Avatar
victoriasmith1986 Mar 19, 2026

๐Ÿ“š Quick Study Guide: JS Variables

  • ๐Ÿ’ก var Keyword:
    • ๐ŸŒ Scope: Function-scoped or global-scoped.
    • ๐Ÿ”„ Re-declaration/Re-assignment: Can be re-declared and re-assigned within its scope.
    • โฌ†๏ธ Hoisting: Hoisted to the top of its scope and initialized with `undefined`.
    • ๐Ÿšซ Modern Use: Generally discouraged in modern JS due to potential for bugs.
  • ๐Ÿ”‘ let Keyword:
    • ๐Ÿงฑ Scope: Block-scoped (e.g., inside `{}`).
    • โŒ Re-declaration: Cannot be re-declared in the same scope.
    • โœ… Re-assignment: Can be re-assigned.
    • โฐ Hoisting/TDZ: Hoisted, but not initialized. Accessing before declaration results in a `ReferenceError` (Temporal Dead Zone - TDZ).
  • ๐Ÿ”’ const Keyword:
    • ๐Ÿ—๏ธ Scope: Block-scoped.
    • ๐Ÿšซ Re-declaration/Re-assignment: Cannot be re-declared or re-assigned after initial declaration.
    • โš ๏ธ Initialization: MUST be initialized at the time of declaration.
    • โณ Hoisting/TDZ: Also subject to the Temporal Dead Zone (TDZ).
    • โœจ Immutability (Objects): For objects, `const` prevents re-assignment of the variable itself, but the properties of the object can still be modified.
  • โš–๏ธ Key Differences Summary:
    • ๐ŸŽฏ Scoping: `var` (function/global), `let`/`const` (block).
    • ๐Ÿ”„ Flexibility: `var` (most flexible), `let` (re-assignable, not re-declarable), `const` (neither).
    • ๐Ÿ›ก๏ธ Best Practice: Prefer `const` by default, use `let` when re-assignment is necessary, avoid `var`.

๐Ÿง  Practice Quiz: JavaScript Variables

1. Which keyword allows re-declaration and re-assignment within its scope?

2. What is the scope of a variable declared with let?

3. Which keyword declares a constant variable that must be initialized at declaration and cannot be re-assigned?

4. Consider the following code:

console.log(x); // What will this output?
var x = 10;

5. Which of the following statements about let is true?

6. What happens if you try to re-assign a const variable after its initial declaration?

const PI = 3.14;
PI = 3.14159; // What will happen?

7. Which variable declaration keyword is generally considered best practice in modern JavaScript development due to its block-scoping and predictable behavior?

Click to see Answers

1. C (var allows both re-declaration and re-assignment)

2. C (let is block-scoped)

3. C (const declares a constant variable)

4. B (var is hoisted and initialized with undefined)

5. C (let is hoisted but not initialized, leading to TDZ)

6. B (Re-assigning a const variable causes a TypeError)

7. B (let and const are preferred for their block-scoping and clearer behavior)

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! ๐Ÿš€