Learn / Programming / JavaScript

So meanwhile... make it do things.

Meet JavaScript, the language that makes web pages respond. Change a program’s values and watch what it does.

15 minutes, beginner

JavaScript: your first program

By the end you’ll know what variables, choices and loops are, and you’ll have changed a real program to make it behave differently.

1. Say it

A program is a list of instructions the computer follows in order. These are the building blocks you’ll meet today.

JavaScriptWhat it doesExample
letMakes a variable, a labelled box that holds a valuelet age = 15;
"text"A piece of text, called a string, always in quotes"Sam"
15A number, with no quoteslet age = 15;
console.log()Prints a messageconsole.log("Hi");
if and elseMakes a choice between two pathsif (age >= 18) { ... }
forRepeats something, called a loopfor (let i = 1; i <= 3; i++)
+Joins text, or adds numbers"Hi " + name

Tip: Lines usually end with a semicolon, like full stops in a sentence. Text goes in quotes. Numbers don’t.

2. Know it

Together, the three web languages split the work. HTML says what is on a page, CSS says how it looks, and JavaScript says what it does. It runs inside your browser, which is how pages respond when you click, type or scroll.

Numbers and text behave differently, which surprises almost every beginner. In JavaScript, 5 + 1 gives 6, but "5" + 1 gives "51", because the quotes make the 5 into text, and plus then joins text instead of adding.

When a program runs, it goes line by line from the top. The first three lines below store values. Everything after them uses those values, so changing one value changes the whole result.

3. Use it

Change the three highlighted values, then press Run to see what the program prints. Then try the challenges.

// Change the three values, then press Run.
let name = "";
let age = ;
let times = ;

console.log("Hello, " + name + "!");
console.log("Next year you will be " + (age + 1) + ".");

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are under 18.");
}

for (let i = 1; i <= times; i++) {
  console.log("Counting: " + i);
}

What the program prints:

Press Run to see what the program prints.
  • Challenge 1: change the name to your own.To do
  • Challenge 2: make the program print “You are an adult.” (Which value decides that?)To do
  • Challenge 3: make it count up to 5.To do

Notice: You changed three values and the whole program responded. The name changed the greeting, the age changed the choice and the number of times changed the loop. That’s what variables are for.

This page runs a ready-made program, and the three values are yours to change. That keeps it safe, and nothing you type is saved or sent anywhere. To run your own JavaScript later, a browser’s built-in developer tools work well.

4. Check it

1. What does console.log("Hi") do?

2. In let age = 15;, what is age?

3. What is the difference between "5" and 5?

4. Which word makes a program choose between two paths?

5. What does a for loop do?

Done!

You’ve met variables, choices and loops, and changed a program to make it behave differently.

Go further