Loading video player…

Change Outer Variables Carefully

In this video, you counted the votes cast for Priya’s game night with a helper that changed a variable outside it. You saw where an assignment without let or const really goes, and you rewrote the helper to take the count in and hand the new one back.

Key Points

  • A function makes a box for its parameters and the names declared inside it, and main.js as a whole is a box too. When a line reads a name, JavaScript looks in that line’s own box first, then walks outward until it finds the name.
  • An assignment without let or const does the same walk as a read. The box of recordVote declares no votesCast, so votesCast = votesCast + 1; changes the counter in main.js, and two calls log Votes cast: 2.
  • main.js loads as a module, and a module always runs in strict mode, a set of stricter rules for that file. One of them says that an assignment to a name declared nowhere stops the script.
  • Past main.js there’s one last box, the page, which holds the browser’s own names. If you forget the let on votesCast = 0;, the walk finds no votesCast even there, and the Console shows a ReferenceError: Uncaught ReferenceError: votesCast is not defined.
  • The Console isn’t strict. There, votesCast = 0 gives back 0 with no error, because it quietly makes a new variable in the page box.
  • Watch out: strict mode catches only names declared nowhere. The page box already holds status and keeps it as text. So status = 2; in main.js gives no error. Your 2 becomes "2", and status + 1 joins them into 21.
  • Watch out if you add let inside the function to leave the outer counter alone: let votesCast = votesCast + 1; throws Uncaught ReferenceError: Cannot access 'votesCast' before initialization. The walk stops at the first box that declares the name, now the box of recordVote, and that votesCast isn’t ready until its line finishes, so reading it after the = fails.
  • The first version works, but nothing in the line that defines recordVote tells you it changes the counter outside. recordVote is a toy. On a real page with a dozen helpers, you’d read every body to find which one changes votesCast.
  • Instead, hand the count in and take the new one back. recordVote gets a parameter, tally, and its concise body returns tally + 1. It changes nothing outside itself, and only the lines that call it change votesCast.
  • votesCast is still an outer variable, on purpose, because it’s what Priya’s page must remember. Later on, when buttons count the votes, that one outer variable keeps the count between clicks.
  • As a rule, use let or const to declare every name. A function hands results back with return, and takes values in through its parameters.

The Final Code

Here’s the end of main.js after this video. Copy it if you want to compare your own work, or to start the next lesson from the same place:

Language: JavaScript Filename: scratch/main.js
let votesCast = 0;

const recordVote = (tally) => tally + 1;

votesCast = recordVote(votesCast);
votesCast = recordVote(votesCast);
console.log("Votes cast:", votesCast);

When you save, the Console shows Votes cast: 2.

The version you started from changed the counter from inside the function, which works but hides the change in the function’s body:

Language: JavaScript
let votesCast = 0;

const recordVote = () => {
  votesCast = votesCast + 1;
};

recordVote();
recordVote();
console.log("Votes cast:", votesCast);

Follow Along in a Blank Tab

If you don’t have Priya’s files, open a new tab, go to about:blank, and open the Console with Ctrl+Shift+J, or Cmd+Option+J on macOS. Type each entry rather than pasting it, because Chrome blocks a first paste into the Console as a safety measure. Press Shift+Enter between the lines of an entry.

First, the counter and the version of recordVote that changes it from inside:

Language: JavaScript
> let votesCast = 0;
undefined
> const recordVote = () => {
…   votesCast = votesCast + 1;
… };
undefined
> recordVote();
undefined
> recordVote();
undefined
> console.log("Votes cast:", votesCast);
Votes cast: 2
undefined

Reload the tab to clear those entries, then type the version that takes the count in and hands the new one back:

Language: JavaScript
> let votesCast = 0;
undefined
> const recordVote = (tally) => tally + 1;
undefined
> votesCast = recordVote(votesCast);
1
> votesCast = recordVote(votesCast);
2
> console.log("Votes cast:", votesCast);
Votes cast: 2
undefined

The missing-let error is the one thing a blank tab can’t show. It needs strict mode, which comes with a module script like main.js, and the Console isn’t one. So you’ll see that ReferenceError only on Priya’s page.

00:00 In Write Arrow Functions, each arrow you wrote worked only with its parameters. In this video, you’ll see what happens when a function changes a variable that lives outside it.

00:11 Priya wants to know how many votes have been cast for what to play first. At the end of main.js, add a counter, votesCast, starting at zero. It’s a let, because it will change.

00:23 Then add an arrow called recordVote, stored in a const like the ones you wrote before. Its body adds one to the counter. Call it twice, for two votes, and log the count.

00:36 A function makes a box for its parameters and the names declared inside it. main.js as a whole is a box too, and recordVote’s box sits inside it. When a line reads a name, JavaScript looks in that line’s own box first, then walks outward until it finds the name.

00:54 So the votesCast after the equals sign is the counter outside. The left side has no let, though. Your turn: pause the video and predict what the log line shows.

01:11 Save, and the Console shows Votes cast: 2.

01:16 An assignment without let or const does the same walk as a read. recordVote’s box declares no votesCast, so the walk leaves the function. It finds the counter in main.js and changes that one.

01:30 The first call takes it to one, and the second call to two.

01:36 Now say you forget the let on the counter’s first line. The walk passes main.js. Past it there’s one last box, the page, which holds the browser’s own names.

01:47 It has no votesCast either. main.js loads as a module, and a module always runs in strict mode, a set of stricter rules for that file. One of its rules says that an assignment to a name declared nowhere stops the script.

02:04 That’s all you need about strict mode for now. Save, and the Console shows a ReferenceError, saying votesCast isn’t defined.

02:13 The Console isn’t strict. Type the same line there, and it gives back zero, with no error. It quietly made a new variable in the page box. Watch out: strict mode catches only names declared nowhere.

02:28 The page box already holds status and keeps it as text. So status equals 2 in main.js gives no error. Your 2 becomes the text 2, and plus one joins them into 21.

02:42 Back to the working version, with the let in place. It counts correctly, but nothing in recordVote’s own line tells you it changes the counter outside. recordVote is a toy.

02:53 On a real page with a dozen helpers, you’d read every body to find which one changes votesCast. Watch out if you add let inside the function to leave the outer counter alone: the walk then stops at recordVote’s own votesCast, which isn’t ready until its line finishes, so that line throws an error.

03:14 Instead, hand the count in and take the new one back. Give recordVote a parameter, tally. Its body becomes tally plus one, a concise arrow, so it returns that value.

03:26 It changes nothing outside itself. At each call, pass votesCast in, and store what comes back in the same variable. Save, and the Console shows Votes cast: 2 again.

03:40 votesCast is still an outer variable, and it should be, because it’s what Priya’s page must remember. The difference is that only the lines that call recordVote change it, and you can see them.

03:51 Later on, when buttons count the votes, that one outer variable will keep the count between clicks.

03:58 As a rule, use let or const to declare every name. A function hands results back with return, and takes values in through its parameters.

04:09 Next, in Pass Functions to Other Functions, you’ll hand a function to another function, which then decides when to call it.

Become a Member to join the conversation.