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.jsas 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
letorconstdoes the same walk as a read. The box ofrecordVotedeclares novotesCast, sovotesCast = votesCast + 1;changes the counter inmain.js, and two calls logVotes cast: 2. main.jsloads 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.jsthere’s one last box, the page, which holds the browser’s own names. If you forget theletonvotesCast = 0;, the walk finds novotesCasteven there, and the Console shows aReferenceError:Uncaught ReferenceError: votesCast is not defined. - The Console isn’t strict. There,
votesCast = 0gives back0with 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
statusand keeps it as text. Sostatus = 2;inmain.jsgives no error. Your2becomes"2", andstatus + 1joins them into21. - Watch out if you add
letinside the function to leave the outer counter alone:let votesCast = votesCast + 1;throwsUncaught ReferenceError: Cannot access 'votesCast' before initialization. The walk stops at the first box that declares the name, now the box ofrecordVote, and thatvotesCastisn’t ready until its line finishes, so reading it after the=fails. - The first version works, but nothing in the line that defines
recordVotetells you it changes the counter outside.recordVoteis a toy. On a real page with a dozen helpers, you’d read every body to find which one changesvotesCast. - Instead, hand the count in and take the new one back.
recordVotegets a parameter,tally, and its concise body returnstally + 1. It changes nothing outside itself, and only the lines that call it changevotesCast. votesCastis 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
letorconstto declare every name. A function hands results back withreturn, 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:
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:
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:
> 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:
> 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.