0
0
Javascriptprogramming~5 mins

Block scope in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is block scope in JavaScript?
Block scope means variables declared inside a pair of curly braces { } are only accessible within those braces.
Click to reveal answer
beginner
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.
Click to reveal answer
beginner
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.
Click to reveal answer
beginner
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.
Click to reveal answer
beginner
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.
Click to reveal answer
Which keyword creates a variable with block scope?
Aglobal
Blet
Cfunction
Dvar
What will happen if you access a block-scoped variable outside its block?
AIt works fine
BReturns undefined
CVariable is hoisted globally
DReferenceError is thrown
Which of these is NOT block scoped?
Aconst
Blet
Cvar
Dvariables inside { }
Why prefer let or const over var?
AThey have block scope, reducing bugs
BThey are faster
CThey are global
DThey allow redeclaration
What does block scope help prevent?
AVariable name conflicts
BSyntax errors
CInfinite loops
DMemory leaks
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.