Picture a shelf of board games and a few friends coming over tonight. Before anyone opens a box, you want to know which games fit the group and the evening. Each game comes with several facts: its title, how many people can play, and its playing time. A useful program has to keep those facts together and tell them apart.
That’s what this course is about. You’ll learn to describe one thing using a record, which groups named facts together, and then work with whole lists of records to answer questions such as “Which games can four people finish in an hour?”
By the end of the course, you’ll build a board game shelf helper in on-site exercises that check your code for you. After that comes a small final project of your own on your computer, such as a game night planner, a packing budget helper, or a quiz score report.
Work Through This Course
This course builds on the earlier JavaScript Basics courses. You’ll use variables, strings, arrays, loops, and small functions that count, filter, or search through an array. If you’ve arrived here directly, those are the skills to bring along. You don’t need any knowledge about board games, because every example gives you all the data it uses.
You’ll keep working the way you’ve worked before. Open your course folder in VS Code, open the integrated terminal there, and keep a small file in the scratch/ folder. You’ll type each example, run it with node, change something, and run it again.
Text lessons like this one show you an idea in a small program. On-site exercises then let you practice it with automated feedback. Along the way, optional buttons let you ask the mentor a focused question, and you can follow every lesson without using them.
Start From an Array You Already Know
You already know one way to keep several values together: an array. Here’s one game stored that way, with its title, its maximum number of players, and its playing time in minutes. Create scratch/game.js and type it in:
scratch/game.js
const game = ["Ticket to Ride", 5, 60];
console.log(game[2]);
Before you run it, decide which value this line prints and what that number means for your game night. Then save the file and run it in the terminal:
$ node scratch/game.js
60
The value at index 2 is 60, the playing time. You can only tell because you read the line that creates the array and count positions from zero. Nothing in game[2] itself says “minutes.”
Now imagine you mix up the positions and write game[1] when you meant the playing time. JavaScript happily prints 5, and your program might decide that a long game takes only five minutes. The array can’t help you here, because its positions have numbers, not meanings.
Give Each Value a Name
A record solves this by storing each value under a name. In JavaScript, you write a record as an object literal, a pair of curly braces holding name: value pairs separated by commas. Each pair is a field of the record. Replace the contents of scratch/game.js with this version:
scratch/game.js
const game = {
title: "Ticket to Ride",
maxPlayers: 5,
minutes: 60,
};
console.log(game.minutes);
The record has three fields named title, maxPlayers, and minutes. The name of each field sits to the left of the colon, and its value sits to the right. Field names follow the same naming style as your variables, so maxPlayers uses camelCase.
To read a field, you write the record’s name, a dot, and the field’s name. This is called dot notation. Run the script again:
$ node scratch/game.js
60
You get the same 60 as before, but this time the code says which fact you want. Anyone who reads game.minutes knows it’s the playing time without counting positions.
JavaScript’s own name for this kind of value is an object, and its fields are officially called properties. In this course, you’ll use the terms record and field when an object describes one thing.
You can also log the whole record to see all its fields at once. Change the last line to console.log(game); and run the script:
scratch/game.js
const game = {
title: "Ticket to Ride",
maxPlayers: 5,
minutes: 60,
};
console.log(game);
$ node scratch/game.js
{ title: 'Ticket to Ride', maxPlayers: 5, minutes: 60 }
Node.js shows the record on one line and puts strings in single quotes. That’s only Node.js’s display style, and the double quotes in your code are fine.
Make the Record Your Own
Now it’s your turn to change the record. Replace the three values with a game you know or any game you’d like to invent. Then use the fields in a sentence, joining strings with + as you’ve done before:
scratch/game.js
const game = {
title: "Ticket to Ride",
maxPlayers: 5,
minutes: 60,
};
console.log(game.title + " takes " + game.minutes + " minutes.");
Running this example produces the following output:
$ node scratch/game.js
Ticket to Ride takes 60 minutes.
Run it with your own values and check that the sentence matches the record you wrote. If you add a fourth field to the literal, such as minPlayers: 2, then you can mention it in the sentence too.
You might also see a second way of reading a field, with the name as a string inside square brackets. For example, game["title"] reads the same field as game.title. Dot notation is shorter, so you’ll use it whenever you know the field name while writing the code. Brackets become more useful in a later lesson.
Read a Field That Isn’t There
Field names have to match exactly, including capital letters. So game.minutes and game.Minutes look up two different names, and this record only has the first one.
When you ask a record for a field it doesn’t have, JavaScript doesn’t stop with an error. Instead, the result is undefined, a special value that means “there’s no value here.” Try three lines that each ask for the playing time:
scratch/game.js
const game = {
title: "Ticket to Ride",
maxPlayers: 5,
minutes: 60,
};
console.log(game.minutes);
console.log(game.Minutes);
console.log(game.minuts);
Before you run it, predict what each of the three lines prints. Then compare your answers with the output:
$ node scratch/game.js
60
undefined
undefined
Only the first line finds the field. The lines with the capital M and the missing e try to read fields that don’t exist, so both print undefined.
This quiet result is easy to overlook, so treat undefined as information. With this game record, an undefined result means you asked for a name the record doesn’t have. In general, undefined from a field read is a sign that the field may be missing or misspelled, though it doesn’t prove it. A useful first check is the spelling and capitalization of the field name, compared letter by letter with the record.
Build a Fresh Record for a Result
Records aren’t only for data you’re handed. When your program produces a result that has several parts, you can package that result as a new record too. For example, say you want a small label to stick on the game box, with a name and a playing time written out as text:
scratch/game.js
const game = {
title: "Ticket to Ride",
maxPlayers: 5,
minutes: 60,
};
const label = {
name: game.title,
playTime: game.minutes + " minutes",
};
console.log(label);
console.log(game);
The label record gets its own field names, chosen for what the label shows. Each value comes from the source record. The name field copies game.title, and the playTime field joins game.minutes with a string. Run it to see both records:
$ node scratch/game.js
{ name: 'Ticket to Ride', playTime: '60 minutes' }
{ title: 'Ticket to Ride', maxPlayers: 5, minutes: 60 }
The label is a separate record with exactly the fields you wrote. The second line shows that game still has its original three fields. Building the label reads values from game without changing it, which is what you’ll want whenever a result is based on data you were given.
Now build one yourself. Below your label, add a record named invite for a message to your friends. Give it a game field holding the game’s title and a players field holding a string such as "up to 5 players", built from game.maxPlayers. Log invite and check that each field shows the value you expected.
Before you run your version, say out loud which field of game supplies each field of invite. If a field shows undefined, then you’ve found a name that doesn’t match the record, and you know where to look.
Check Your Understanding
Take a moment with this record before you move on:
const recipe = {
dish: "Pancakes",
servings: 4,
minutes: 20,
};
Without running anything, decide what recipe.servings, recipe.Dish, and recipe.price give you, and why. Then write a fresh record named card with a heading field holding the dish and a time field holding a string such as "20 minutes".
To check yourself, paste the record into a scratch file and add your card below it. Then add five console.log() calls, one for each of the three reads, one for card, and one for recipe, and run the file.
You should see 4, then undefined twice, because Dish has the wrong capital letter and the record has no price field at all. Your card should show heading: 'Pancakes' and time: '20 minutes', and recipe should still have its original three fields.
Practice On-Site
In the next item, you’ll read and build records in an on-site coding exercise. You’ll write and run code right in your browser, and automated tests check your result and tell you what’s still missing. Because it’s the course’s first exercise, it opens with a short orientation to the editor and the tests.
To get feedback, click the Run Tests button, or press Cmd+Enter on macOS or Ctrl+Enter on other systems. It runs the exercise’s supplied checks against your code and shows which ones passed and which failed.
Run the tests early and often, even before your code is finished. When a check fails or an error appears, read its message for clues, such as a field name that doesn’t match. Change one thing in your code, then click Run Tests again to see whether that check passes now. If you’d like more room to work, use the Full screen button.
Next up is Read and Build a Library Book Card, where you’ll read fields from a book record and build a fresh label record from them.