We use let to create a variable that can change later. It helps us store and update information in our program.
Variable declaration using let in Javascript
Introduction
Syntax
Javascript
let variableName = initialValue;let creates a variable that can be changed later.
You can declare a variable without giving it a value first: let x;
Examples
age and sets it to 25.Javascript
let age = 25;
score is declared first without a value, then given the value 10.Javascript
let score; score = 10;
count after declaring it.Javascript
let count = 0; count = count + 1;
Sample Program
This program shows how a let variable can change its value and print both values.
Javascript
let name = "Alice"; console.log(name); name = "Bob"; console.log(name);
Important Notes
Variables declared with let are block-scoped, meaning they only exist inside the nearest curly braces { }.
Using let helps avoid errors that happen when variables are used outside their intended area.
Summary
let creates variables that can change value.
It limits where the variable can be used to keep code safe and clear.
Use let instead of older var for better coding habits.
Practice
1. What does the
let keyword do in JavaScript?easy
Solution
Step 1: Understand
letbehaviorletdeclares variables that can be reassigned new values.Step 2: Understand scope of
Variables declared withletletare limited to the block they are declared in (block scope).Final Answer:
Declares a variable that can change value and has block scope -> Option BQuick 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
Solution
Step 1: Recall correct variable declaration syntax
The correct syntax is:let variableName = value;Step 2: Match syntax with options
let score = 10; matches the correct syntax exactly.Final Answer:
let score = 10; -> Option DQuick 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
Solution
Step 1: Understand variable assignment and reassignment
Variablexis first set to 5, then changed to 10.Step 2: Check console.log output
Sincexwas reassigned to 10,console.log(x)prints 10.Final Answer:
10 -> Option AQuick 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
Solution
Step 1: Understand block scope of let
Variablemessageis declared inside the if block and only exists there.Step 2: Check console.log outside block
Trying to accessmessageoutside the block causes a ReferenceError.Final Answer:
ReferenceError: message is not defined -> Option AQuick 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:
What will be printed and why?
let total = 0;
for (let i = 0; i < 3; i++) {
let total = i * 2;
}
console.log(total);What will be printed and why?
hard
Solution
Step 1: Identify variable scopes
There are twototalvariables: one outside the loop and one declared inside the loop block.Step 2: Understand variable shadowing
The innertotalinside the loop is a new variable that does not affect the outertotal.Step 3: Check final console.log
The outertotalremains 0, soconsole.log(total)prints 0.Final Answer:
0, because the outer total is unchanged by inner block -> Option CQuick 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
10 questions ~5 mineasy hard
