Functions – Scope

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Functions – Scope

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does a variable declared with let inside a function stay local to it?
A Yes
B No
2. Can code outside a function access a variable declared inside it?
A No
B Yes
3. Does each function create its own separate scope?
A Yes
B No
4. Can a variable be used before it's declared inside its own scope?
A Yes
B No
5. Does calling a function twice create two separate variable scopes?
A No
B Yes

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1function greet() { let name = "Sam"; } AWrong - name is scoped inside greet and isn't accessible outside it
2function calc() { let total = 10; return total; } Blet count = 5;
3The area of code where a variable can be accessed CDeclares name as a variable scoped inside greet
4Keeps a variable local so it can't be accessed outside its function Dscope
5function greet() { let name = "Sam"; } console.log(name); EDeclares total as a variable scoped inside calc
6function calc() { let total = 5; } console.log(total); FWrong - total is scoped inside calc and isn't accessible outside it

Definitions

scope

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
function greet() { ___ name = "Sam"; console.log(name); }
2.
function calc() { let total = 10; ______ total; } console.log(calc());
3.
function show() { ___ count = 5; console.log(count); }
4.
function test() { if (true) { ___ x = 1; console.log(x); } }
5.
function calcArea(side) { let area = side * side; ______ area; } console.log(calcArea(4));

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A console.log(msg);
B function say() {
C let msg = "Hi";
D }
Question 2
A console.log(result);
B function add(a, b) {
C let result = a + b;
D return result;
E }
Question 3
A function greet() {
B console.log(user);
C let user = "Amy";
D }
Question 4
A function calcArea(s) {
B console.log(area);
C let area = s * s;
D return area;
E }
Question 5
A function outer() {
B let x = 1;
C console.log(y);
D }

Answer Key

Functions – Scope
Section A: Quick Questions
Q1
A — Yes
Q2
A — No
Q3
A — Yes
Q4
B — No
Q5
B — Yes
Section B: Match the Pairs
Q1
function greet() { let name = "Sam"; } → Declares name as a variable scoped inside greet
Q2
function calc() { let total = 10; return total; } → Declares total as a variable scoped inside calc
Q3
The area of code where a variable can be accessed → scope
Q4
Keeps a variable local so it can't be accessed outside its function → let count = 5;
Q5
function greet() { let name = "Sam"; } console.log(name); → Wrong - name is scoped inside greet and isn't accessible outside it
Q6
function calc() { let total = 5; } console.log(total); → Wrong - total is scoped inside calc and isn't accessible outside it
Section C: Definitions
Q1
scope — The area of code where a variable can be accessed; variables declared inside a function are local to it.
Section D: Fill in the Blanks
Q1
function greet() { let name = "Sam"; console.log(name); }
Q2
function calc() { let total = 10; return total; } console.log(calc());
Q3
function show() { let count = 5; console.log(count); }
Q4
function test() { if (true) { let x = 1; console.log(x); } }
Q5
function calcArea(side) { let area = side * side; return area; } console.log(calcArea(4));
Section E: Spot the Error
Q1
A — msg is scoped inside say() and isn't accessible before or outside it.
Q2
A — result is scoped inside add() and isn't accessible outside it.
Q3
B — user is used before it's declared inside the function's scope.
Q4
B — area is used before it's declared inside the function's scope.
Q5
C — y was never declared anywhere in scope, so it can't be used.