Function Scope

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Every Call Starts Fresh

Suppose you write a `fight()` function with a local variable `let turns = 0` inside it to count how many turns have passed. If you call `fight()` three times in a row, you might expect `turns` to reach `3`—but look at what actually happens:

function fight() { let turns = 0; turns = turns + 1; console.log("Turns: " + turns); } fight(); fight(); fight();

It prints `1` every single time! That's because every function call creates a brand-new set of local variables, runs the code, and destroys those local variables as soon as the function finishes. Nothing inside a normal function call is kept for the next call.

Exercise 2 — Keeping a Value Between Function Calls

If you want a counter to keep growing across multiple calls, the variable must live outside the function so it isn't destroyed when the function finishes:

let turns = 0; function fight() { turns = turns + 1; console.log("Turns: " + turns); } fight(); fight(); fight();

Now `turns` reaches `3`! However, because `turns` is a global variable, any other part of your code could accidentally change it. A cleaner way to update a value without relying on a global variable inside the function is to pass the current value as a parameter and return the updated value:

function nextTurn(turns) { return turns + 1; } let count = 0; count = nextTurn(count); count = nextTurn(count); console.log("Turns: " + count);

Exercise 3 — Functions Inside Functions

Just like you can declare local variables inside a function, you can also declare a helper function inside another function! An inner function follows the exact same scope rules as a local variable: it can only be called inside the outer function where it was created:

function battle() { function announce(name) { console.log(name + " attacks!"); } announce("Zap"); announce("Aquachu"); } battle(); announce("Pyrochu"); // error!

This is handy when a small helper function is only needed inside one specific function—and it sets the stage for one of JavaScript's most powerful features, which we'll explore next!

Exercise 4 — Quiz: Same Result Every Time

What does this code print?

function count() { let n = 0; n++; console.log(n); } count(); count();
Options
• 1 then 1
• 1 then 2
• 0 then 1
• An error

Hint : Each call rebuilds its local variables from scratch.

Exercise 5 — Quiz: The Outer Counter

What does the last line print?

let n = 0; function count() { n++; } count(); count(); console.log(n);
Options
• 2
• 1
• 0
• An error

Hint : This variable is not destroyed when a call ends.

Exercise 6 — Quiz: The Inner Function

What happens on the last line?

function outer() { function inner() { console.log("hi"); } inner(); } outer(); inner();
Options
• hi, then an error
• hi twice
• An error straight away
• Nothing

Hint : An inner function is local to the one that holds it.

Exercise 7 — Make It Survive

Move the counter so it keeps its value between the two calls.

Hint : The variable must live outside and be allowed to change, and the function must reach that same one.

Exercise 8 — Function Scope Cheat Sheet

Match each situation with what happens.

Items to match
• A local declared in the function
• A variable declared outside
• A function declared inside another
• A parameter
Definitions
• Rebuilt at every call
• Survives between calls
• Callable only from inside
• A fresh copy at every call

Exercise 9 — Battle Tracker

Track a battle where the turn count must survive across calls, while every temporary value stays local.

Step 1: `turns` is declared outside the function so it survives, starting at 0. Step 2: `playTurn(base, critical)` increases `turns`, then declares a local `damage` equal to the base, doubled when the hit is critical. Step 3: inside `playTurn`, declare an inner function `announce(value)` that prints `"Turn N: X damage"` using the current turn count. Call it, then return the damage. Step 4: play a normal turn with base 20, a critical turn with base 20, then print `"Total turns: N"`.

Expected output:

Turn 1: 20 damage Turn 2: 40 damage Total turns: 2
Starter code
let turns = 0; // Write your function here // Use it here
Back to the course