← JavaScript

Functions – Scope

The area of code where a variable can be accessed; variables declared inside a function are local to it.

Understanding Functions – Scope in JavaScript

What it does: scope describes exactly where a variable is visible and usable. Declaring with let inside a function scopes the variable to it. The variable only exists within that function's scope. Once the function ends, its local variables are gone. Variables in one function don't interfere with another function's variables. A variable must be declared before it can be used. Functions can access variables from the scope they were defined in. Once greet() finishes running, name no longer exists.

Common mistakes: msg is scoped inside say() and isn't accessible before or outside it. result is scoped inside add() and isn't accessible outside it.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 41–52) and word search (Modules 21–52).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Does a variable declared with let inside a function stay local to it?
YesNo
Matches
function greet() { let name = "Sam"; } console.log(name);
↓
Wrong - name is scoped inside greet and isn't accessible outside it
Applying
Meanings
Term
scope
Meaning
The area of code where a variable can be accessed; variables declared inside a function are local to it.
Practice
function greet() {
Testing
Gaps
name = "Sam";
Check Answer
Spots
1console.log(msg);
2function say() {

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Ermine' badge — Tundra Streak, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Does a variable declared with let inside a function stay local to it?
A. Yes ✓
B. No
The variable only exists within that function's scope.
2. Can code outside a function access a variable declared inside it?
A. No ✓
B. Yes
Once the function ends, its local variables are gone.
3. Does each function create its own separate scope?
A. Yes ✓
B. No
Variables in one function don't interfere with another function's variables.
4. Can a variable be used before it's declared inside its own scope?
A. Yes
B. No ✓
A variable must be declared before it can be used.
5. Does calling a function twice create two separate variable scopes?
A. No
B. Yes ✓
Each call gets its own fresh copy of the function's local variables.

Match the Pairs

function greet() { let name = "Sam"; } → Declares name as a variable scoped inside greet
function calc() { let total = 10; return total; } → Declares total as a variable scoped inside calc
The area of code where a variable can be accessed → scope
Keeps a variable local so it can't be accessed outside its function → let count = 5;
function greet() { let name = "Sam"; } console.log(name); → Wrong - name is scoped inside greet and isn't accessible outside it
function calc() { let total = 5; } console.log(total); → Wrong - total is scoped inside calc and isn't accessible outside it

Key Term

scope
The area of code where a variable can be accessed; variables declared inside a function are local to it.

Fill in the Blanks

1. function greet() { let name = "Sam"; console.log(name); }
Hint: Type the keyword used to declare a local variable.
2. function calc() { let total = 10; return total; } console.log(calc());
Hint: Type the keyword that sends a value back out of the function.
3. function show() { let count = 5; console.log(count); }
Hint: Type the keyword used to declare a local variable.
4. function test() { if (true) { let x = 1; console.log(x); } }
Hint: Type the keyword used to declare a local variable.
5. function calcArea(side) { let area = side * side; return area; } console.log(calcArea(4));
Hint: Type the keyword that sends a value back out of the function.

Spot the Error

Question 1
A console.log(msg);
B function say() {
C let msg = "Hi";
D }
Line A — msg is scoped inside say() and isn't accessible before or outside it.
Question 2
A console.log(result);
B function add(a, b) {
C let result = a + b;
D return result;
E }
Line A — result is scoped inside add() and isn't accessible outside it.
Question 3
A function greet() {
B console.log(user);
C let user = "Amy";
D }
Line B — user is used before it's declared inside the function's scope.
Question 4
A function calcArea(s) {
B console.log(area);
C let area = s * s;
D return area;
E }
Line B — area is used before it's declared inside the function's scope.
Question 5
A function outer() {
B let x = 1;
C console.log(y);
D }
Line C — y was never declared anywhere in scope, so it can't be used.

Student with a class code? Join your class →