Code is a set of instructions written so a computer can follow them exactly. Computers are fast, but they do only what they are told, in the order they are told.
Every app you use, from your phone’s alarm to a bus tracker, is made of instructions like these. People who write them are called programmers or software developers.
A program is a list of instructions.
A programming language is the set of words and rules you write them in.
JavaScript runs in every web browser, so it is a great first language.
Storing information · 15 minutes
A variable is a labeled box that holds a value. You make one with the word let, give it a name, and put something in it.
let name = "Jordan";
let hours = 3;
hours = hours + 2; // hours is now 5
console.log(name + " served " + hours + " hours");
Text goes inside quotes and is called a string. Numbers do not need quotes. The + sign adds numbers and joins strings.
Interactive coding exercise: open the interactive version to try it.
Making decisions · 20 minutes
Programs choose what to do with if and else. The computer checks a condition. If it is true, one block runs. Otherwise, the other block runs.
let hours = 12;
if (hours >= 10) {
console.log("You earned the 10-hour badge!");
} else {
console.log("Keep going. " + (10 - hours) + " hours to go.");
}
Loops repeat steps. A for...of loop goes through each item in a list, which is how a program adds up all your service hours.
Interactive coding exercise: open the interactive version to try it.