Recall & Review
beginner Click to reveal answer
What is block scope in JavaScript?
Block scope means variables declared inside a pair of curly braces { } are only accessible within those braces.
beginner Click to reveal answer
Which keywords create block-scoped variables in JavaScript?
The keywords
let and const create block-scoped variables. Variables declared with var do not have block scope.beginner Click to reveal answer
What happens if you try to access a block-scoped variable outside its block?
You get a
ReferenceError because the variable does not exist outside the block where it was declared.beginner Click to reveal answer
Example: What will this code output?<br><pre>if (true) { let x = 5; } console.log(x);</pre>It will cause a
ReferenceError because x is declared with let inside the block and is not accessible outside.beginner Click to reveal answer
Why is block scope useful in programming?
Block scope helps keep variables limited to where they are needed, avoiding accidental changes or conflicts with variables elsewhere in the code.
Which keyword creates a variable with block scope?
✗ Incorrect
The
let keyword creates block-scoped variables, while var does not.What will happen if you access a block-scoped variable outside its block?
✗ Incorrect
Accessing a block-scoped variable outside its block causes a
ReferenceError.Which of these is NOT block scoped?
✗ Incorrect
var is function scoped or global scoped, not block scoped.Why prefer
let or const over var?✗ Incorrect
let and const have block scope, which helps avoid bugs caused by variable conflicts.What does block scope help prevent?
✗ Incorrect
Block scope limits variables to a block, preventing conflicts with variables of the same name elsewhere.
Explain block scope and how it differs from function scope in JavaScript.
Think about where variables are accessible inside { } versus inside functions.
You got /4 concepts.
Describe a situation where using block scope helps avoid bugs in your code.
Imagine two parts of code using the same variable name but needing different values.
You got /4 concepts.
