Bird
Raised Fist0
Javascriptprogramming~5 mins

Variable declaration using let in Javascript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the let keyword do in JavaScript?

The let keyword declares a variable that can be changed later. It creates a block-scoped variable, meaning it only exists inside the nearest curly braces { }.

Click to reveal answer
beginner
What is the difference between let and var?

let variables are block-scoped, so they only exist inside the block they are declared in. var variables are function-scoped or global-scoped, which can cause unexpected behavior.

Click to reveal answer
beginner
Can you redeclare a variable using let in the same scope?

No, redeclaring a variable with let in the same scope causes an error. You can only declare it once per block.

Click to reveal answer
intermediate
What happens if you try to use a let variable before it is declared?

You get a ReferenceError because let variables are in a 'temporal dead zone' until their declaration line is reached.

Click to reveal answer
beginner
Example: What will this code output?<br><pre>let x = 5;<br>{<br>  let x = 10;<br>  console.log(x);<br>}<br>console.log(x);</pre>

The first console.log(x) inside the block prints 10 because it uses the inner x. The second prints 5 because it uses the outer x.

Click to reveal answer
What kind of scope does a let variable have?
ABlock scope
BFunction scope
CGlobal scope only
DNo scope
What happens if you declare let x = 1; twice in the same block?
AIt works fine, x is overwritten
Bx becomes undefined
CIt creates two separate variables
DSyntaxError: Identifier 'x' has already been declared
Which keyword allows redeclaration of variables in the same scope?
Alet
Bvar
CNone
Dconst
What error do you get if you use a let variable before declaring it?
AReferenceError
BTypeError
CSyntaxError
DNo error
Which of these is true about let variables?
AThey are not hoisted at all
BThey are hoisted and initialized with undefined
CThey are hoisted but not initialized
DThey behave exactly like <code>var</code>
Explain how let helps avoid bugs compared to var.
Think about where the variable exists and when you can use it.
You got /4 concepts.
    Describe what happens when you declare a variable with let inside a block and outside the block with the same name.
    Imagine a box inside a bigger box, each with its own label.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the let keyword do in JavaScript?
      easy
      A. Declares a constant variable that cannot change
      B. Declares a variable that can change value and has block scope
      C. Declares a variable with global scope only
      D. Declares a function

      Solution

      1. Step 1: Understand let behavior

        let declares variables that can be reassigned new values.
      2. Step 2: Understand scope of let

        Variables declared with let are limited to the block they are declared in (block scope).
      3. Final Answer:

        Declares a variable that can change value and has block scope -> Option B
      4. Quick Check:

        let = variable with block scope [OK]
      Hint: Remember: let = changeable variable inside blocks [OK]
      Common Mistakes:
      • Confusing let with const (constant)
      • Thinking let variables have global scope
      • Mixing let with function declarations
      2. Which of the following is the correct syntax to declare a variable named score using let?
      easy
      A. let = score 10;
      B. let score: 10;
      C. score let = 10;
      D. let score = 10;

      Solution

      1. Step 1: Recall correct variable declaration syntax

        The correct syntax is: let variableName = value;
      2. Step 2: Match syntax with options

        let score = 10; matches the correct syntax exactly.
      3. Final Answer:

        let score = 10; -> Option D
      4. Quick Check:

        Correct syntax = let score = 10; [OK]
      Hint: Use: let variableName = value; [OK]
      Common Mistakes:
      • Placing let after variable name
      • Using colon instead of equal sign
      • Missing semicolon (though optional)
      3. What will be the output of this code?
      let x = 5;
      x = 10;
      console.log(x);
      medium
      A. 10
      B. Error
      C. undefined
      D. 5

      Solution

      1. Step 1: Understand variable assignment and reassignment

        Variable x is first set to 5, then changed to 10.
      2. Step 2: Check console.log output

        Since x was reassigned to 10, console.log(x) prints 10.
      3. Final Answer:

        10 -> Option A
      4. Quick Check:

        Reassigned let variable prints new value [OK]
      Hint: let variables can change value, so print latest [OK]
      Common Mistakes:
      • Thinking let variables cannot be reassigned
      • Expecting original value to print
      • Confusing with const behavior
      4. Find the error in this code:
      if (true) {
        let message = "Hello";
      }
      console.log(message);
      medium
      A. ReferenceError: message is not defined
      B. SyntaxError: Unexpected token
      C. No error, prints Hello
      D. TypeError: message is undefined

      Solution

      1. Step 1: Understand block scope of let

        Variable message is declared inside the if block and only exists there.
      2. Step 2: Check console.log outside block

        Trying to access message outside the block causes a ReferenceError.
      3. Final Answer:

        ReferenceError: message is not defined -> Option A
      4. Quick Check:

        let variables have block scope, so error outside block [OK]
      Hint: let variables only exist inside their block [OK]
      Common Mistakes:
      • Assuming let variables are global
      • Confusing ReferenceError with SyntaxError
      • Trying to access block variables outside
      5. Consider this code:
      let total = 0;
      for (let i = 0; i < 3; i++) {
        let total = i * 2;
      }
      console.log(total);

      What will be printed and why?
      hard
      A. 6, because total sums all i*2 values
      B. 4, because total accumulates inside the loop
      C. 0, because the outer total is unchanged by inner block
      D. ReferenceError, total is redeclared

      Solution

      1. Step 1: Identify variable scopes

        There are two total variables: one outside the loop and one declared inside the loop block.
      2. Step 2: Understand variable shadowing

        The inner total inside the loop is a new variable that does not affect the outer total.
      3. Step 3: Check final console.log

        The outer total remains 0, so console.log(total) prints 0.
      4. Final Answer:

        0, because the outer total is unchanged by inner block -> Option C
      5. Quick Check:

        Inner let shadows outer variable, outer stays 0 [OK]
      Hint: Inner let creates new variable, outer stays same [OK]
      Common Mistakes:
      • Thinking inner total changes outer total
      • Assuming let variables are global
      • Confusing variable shadowing with reassignment
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard