Recall & Review
beginner
What is function scope in JavaScript?
Function scope means variables declared inside a function can only be used within that function. They are hidden from the outside world.
Click to reveal answer
beginner
What happens if you try to access a variable declared inside a function from outside that function?
You get a
ReferenceError because the variable only exists inside the function's scope.Click to reveal answer
beginner
Consider this code:<br><pre>function greet() {<br> let message = 'Hi!';<br> console.log(message);<br>}<br>greet();<br>console.log(message);</pre><br>What will happen when running it?The function prints 'Hi!'. But the last line causes an error because
message is not visible outside the function.Click to reveal answer
beginner
Can variables declared with
var inside a function be accessed outside the function?No. Variables declared with
var inside a function are also limited to that function's scope.Click to reveal answer
intermediate
What is the difference between function scope and block scope?
Function scope limits variables to the whole function. Block scope limits variables to the block (like inside
{}) where they are declared, usually with let or const.Click to reveal answer
Where can a variable declared inside a function be accessed?
✗ Incorrect
Variables declared inside a function are only accessible within that function's scope.
What will happen if you try to access a function-scoped variable outside its function?
✗ Incorrect
Function-scoped variables are not visible outside their function, so accessing them outside causes a
ReferenceError.Which keyword creates a variable with function scope?
✗ Incorrect
var declares variables with function scope.What is the scope of a variable declared with
let inside a function?✗ Incorrect
let variables have block scope, so inside a function they are limited to the block they are declared in.Which of these is true about function scope?
✗ Incorrect
Function scope hides variables declared inside a function from outside access.
Explain function scope in JavaScript and how it controls variable visibility.
Think about where you can use variables declared inside a function.
You got /4 concepts.
Describe the difference between function scope and block scope with examples.
Consider how variables behave inside functions and inside blocks like if or loops.
You got /4 concepts.