Sikau

JavaScript · Day 8 · about 30 min

Variables

Labelled boxes that remember values.

Remembering things

When you save a phone number, you give it a name like "Ama" so you can find it later. Code needs the same thing: a way to remember values by name.

This is one of the most important lessons in the course. Take your time.

Labelled boxes

A variable is a labelled box that holds a value. You create one with the word let, give it a name, and put a value inside with =.

Kitchen jars: Think of jars with labels: "Rice", "Sugar", "Salt". The label is the variable's name; what's inside is its value. You can look inside any time by using the label — and you can swap the contents.

let groceryItem = "rice";
console.log(groceryItem);

groceryItem = "lentils";
console.log(groceryItem);

The = sign doesn't mean "equals" like in maths. It means put this value in that box. Programmers call it assignment.

The rest of this lesson

  • Boxes that never change: constexplain
  • Using variables in mathsexplain
  • Predict the outputpredict
  • Follow the boxpredict
  • Your first variablewrite code
  • Shopping totalwrite code
  • Challengechallenge
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 68 lessons of JavaScript