In this course, you’ll work up to a small but real job. A group of friends orders pizza, and your program works out the bill, each person’s share, and a short message to send around. To get there, your programs need to remember values, calculate with them, and turn them into readable text. This lesson starts with remembering by giving a value a name that your program can use again.
If you’re arriving here directly, you need only a few things from earlier work. You should be able to open a course folder in VS Code, open the integrated terminal there, save a script, run it with node, and try a line in the Node REPL, which later lessons use to check small expressions. If console.log() feels familiar, then you’re ready.
You’ll learn by doing. Text lessons like this one ask you to type code into your own files and run it. Short videos show code running step by step, and on-site exercises check your code automatically. Some sections also offer a question you can send to the mentor to talk something through.
Warm Up: Run a Script Again
Before you learn anything new, check that your setup still works. Open your course folder in VS Code and open the integrated terminal. Then create a file named party.js in your scratch folder and write code that prints six, the number of guests at your pizza party.
Give it a try before you read on. Here’s one way to write it:
scratch/party.js
console.log(6);
Save the file and run it from the terminal:
$ node scratch/party.js
6
If you see 6, then your editor, terminal, and Node.js are all working. If the terminal says it can’t find the file, then check that the terminal is open in your course folder and that you saved the file in scratch.
The script works, but a bare 6 doesn’t tell anyone what it means. Is it six guests, six pizzas, or six dollars? And if the number shows up in several places, then a change of plans means hunting down every copy. Names solve both problems.
Give a Value a Name With let
Replace the line in party.js with these two lines:
scratch/party.js
let guests = 6;
console.log(guests);
Run the script again:
$ node scratch/party.js
6
The output hasn’t changed, but the code now says what the number means. The first line creates a variable, a name that your program can use to get at a value. That line has three parts:
lettells JavaScript that you’re creating a new name. This is called a declaration, and MDN’s page on theletdeclaration has the full details.guestsis the name you chose.= 6points the name at the value6. This is called an assignment.
In the second line, guests has no quotes around it, so JavaScript looks up the name and finds the value it points at. That’s why console.log(guests) prints 6 and not the word guests.
It helps to read = as “now points at” rather than “equals”. You can picture the name as a luggage tag tied to a value. The tag isn’t the value and doesn’t hold a copy. It just tells you where to look. Keep this picture of tags, not boxes, in mind for the rest of the course.
Point a Name at a New Value
Plans change. Two more friends say they’d like to come. You don’t need a new name for that, because you can point guests at a new value. Before you run the next version, predict what each console.log() line will print:
scratch/party.js
let guests = 6;
console.log(guests);
guests = 8;
console.log(guests);
Now run it and compare the output with your prediction:
$ node scratch/party.js
6
8
JavaScript runs the script from top to bottom. When it reaches the first console.log(), guests still points at 6. The third line moves the tag to 8, so the second console.log() finds 8. This is called reassigning the variable. A name always gives you its current value, and nothing remembers that it used to point at 6.
Notice that the third line has no let. You write let exactly once, on the line where you create the name. Every later assignment is a plain =. Try adding let to the third line to see what happens:
scratch/party.js
let guests = 6;
console.log(guests);
let guests = 8;
console.log(guests);
JavaScript refuses to run the script:
$ node scratch/party.js
SyntaxError: Identifier 'guests' has already been declared
The script doesn’t print anything at all, not even the 6 from the line before. JavaScript checks the whole file before it runs any of it, and a SyntaxError means that something in the file isn’t valid JavaScript. The message tells you which name was declared twice, so the fix is to remove the extra let again.
Trace What Each Name Points At
When a program has a few names that change, it helps to trace it. You go through the code line by line and write down what each name points at after each line. Pen and paper is perfect for this.
Tracing matters most when one name is assigned from another. Here’s a short script for planning who does what. Ada hosts, and since she has the biggest fridge, she also brings the drinks. Then the party moves to Grace’s place:
scratch/roles.js
let host = "Ada";
let drinks = host;
host = "Grace";
console.log(host);
console.log(drinks);
Before you run it, remember how assignment works. The right side of = gives a value, and the assignment points the name on the left at that value. In the second line, host gives "Ada", so drinks points at "Ada" too. It points at the value, not at the name host.
Write down what host and drinks point at after each of the first three lines. Then save the script as roles.js in your scratch folder and run it:
$ node scratch/roles.js
Grace
Ada
Here’s the full trace to compare with yours:
| After line | host |
drinks |
|---|---|---|
| 1 | "Ada" |
no value yet (can’t use it) |
| 2 | "Ada" |
"Ada" |
| 3 | "Grace" |
"Ada" |
After line 1, drinks has no value yet, so you can’t use it. A name created with let must get its value before your code reads it. Line 3 moves only the host tag. The drinks tag stays in place, so Ada still brings the drinks. If you expected Grace for both, that’s a common first guess, and the trace shows where the two names separate.
To practice tracing, pick an edit from the Edit the script menu below, such as swapping lines 2 and 3 or having line 3 reassign drinks. For each of the first three lines, pick what you think host and drinks point at, click Reveal to check and watch the tags move, and then click Next line:
Whichever edit you picked, a tag moved only on a line that assigned to that name. The drinks tag keeps the value it got from host until a line assigns to drinks itself.
Explaining a trace in your own words is a good test of whether it makes sense to you. The button below opens a conversation with the mentor and fills in a question for you. You can edit it before sending, for example, to add your own explanation, and the mentor replies and asks follow-up questions:
Choose Names JavaScript Accepts
You get to pick your variable names, but JavaScript has a few rules about what a name can look like:
- A name can contain letters, digits,
_, and$. - A name can’t start with a digit, so
2ndGuestdoesn’t work. - A name can’t contain spaces or dashes.
- A name can’t be a word that JavaScript reserves for itself, such as
let. The JavaScript keywords reference lists them all. - Uppercase and lowercase letters count as different, so
guestsandGuestsare two different names.
When a name breaks a rule, JavaScript stops before running anything and reports a SyntaxError. Here’s what happens when you try to name the number of pizzas with a space in the name:
scratch/pizzas.js
let pizza count = 3;
When you run the script, JavaScript reports a syntax error:
$ node scratch/pizzas.js
SyntaxError: Unexpected identifier 'count'
JavaScript read pizza as the name and then found another word, count, where it expected an =. With a dash instead, as in pizza-count, the message is Unexpected token '-', because JavaScript reads the dash as a minus sign.
To join several words into one name, JavaScript programmers use camelCase. You write the first word in lowercase, start each following word with a capital letter, and leave out the spaces. So the number of pizzas becomes pizzaCount, and the number of slices each person gets becomes slicesPerPerson.
A good name also says what the value is. pizzaCount tells you more than p, x, or number, especially when you come back to a script a week later.
Fix a Name That Doesn’t Match
The most common naming problem doesn’t break any rule. It’s a name that’s spelled slightly differently from its declaration. Type this script into pizzas.js, replacing the line with the space, and run it:
scratch/pizzas.js
let pizzaCount = 3;
console.log(pizzacount);
$ node scratch/pizzas.js
ReferenceError: pizzacount is not defined
This error is a ReferenceError. Here, it means that JavaScript went looking for the name pizzacount and couldn’t find it. The message quotes the name exactly as the code used it. Compare that name with the declaration, letter by letter, and you’ll spot the lowercase c.
Fix the second line so that it matches the declaration, save, and run the script again. Once it prints 3, you’ve done what you’ll do many times as a programmer: read the error message, make one change, and try again.
For more practice, the next button asks the mentor for a fresh broken script. You read it, describe your fix, and the mentor tells you whether that change would work:
Put It Together
To finish, write a small script of your own in scratch/party.js. Plan a party you’d actually enjoy:
- Declare a variable for the host’s name and another for the number of guests, with camelCase names you choose.
- Add a variable that’s assigned from the host’s name, as
drinkswas. - Reassign the host and the number of guests.
- Print all three variables.
Before you run it, trace your script on paper and write down what each console.log() should print. Then run it and compare. If the output matches your trace, then you can read and predict a script that assigns and reassigns values to names.
If a printed value surprises you, trace backward from it. Find the last line that assigned to that name, and check what its right side pointed at on that line. If you still can’t spot the difference, add a temporary console.log() after each assignment and compare each result with your trace.
So far, you’ve pointed names at values that you typed directly into the code. In the next lesson, a short video, you’ll update a value from its own current value, so that guests can become “two more than it is now” instead of a number you work out yourself.