0
0
Javascriptprogramming~5 mins

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

Choose your learning style9 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.