Courses Learn Blog Contact
Get Started →
Lesson 2

Variables

JavaScript Simplified · One concept, one day, one project · Day 2 of 32 Variables How a program remembers information. You learn to store a value under a name, read it back, and change it, then build a profile that print

JavaScript Simplified 5 min read Free

JavaScript Simplified · One concept, one day, one project · Day 2 of 32

Variables

How a program remembers information. You learn to store a value under a name, read it back, and change it, then build a profile that prints your own details.

By the end of today you can

  • Explain what a variable is and why a program needs one
  • Store a value with let and const, and read it back
  • Choose clear variable names and know which names are not allowed
  • Know when a value can change and when it should not

Yesterday your program printed fixed words. A real program needs to remember things: a name, a score, a price, a list. The box a program keeps a value in is called a variable, and today it is the whole lesson.

1. What a Variable Is

Definition

A variable is a named box that holds a value. You put a value in, give the box a name, and later you use the name to get the value back.

Real-Life Scenario

Think of a labelled jar in a kitchen. The jar holds something (sugar), and the label tells you what is inside (the name "sugar"). You do not open every jar to find sugar; you read the label. A variable is the jar, its name is the label, and the value is what is inside.

Making a variable

You create a variable with the word let, then a name, then =, then the value. The = does not mean "equals" here; it means "put this value into this box".

What this code doeslet name = "Ada" stores the text "Ada" under the name name. console.log(name) reads it back out.
app.js
1
2
3
4
// Make a variable called name and put "Ada" in it.
let name = "Ada";
// Use the name to read the value back.
console.log(name);
OUTPUT   TERMINAL
➜ webbo3 node app.js
Ada

2. let and const

Definition

There are two words for making a variable. let makes a box whose value can change later. const makes a box whose value cannot change once set. "const" is short for constant, meaning it stays the same.

Use const by default. It says to anyone reading "this will not change", which prevents accidents. Reach for let only when you know the value needs to change.

What this code doescountry is fixed with const. score is a let, so changing it to 15 is allowed.
app.js
1
2
3
4
5
6
7
// const for a value that will not change.
const country = "Nigeria";
// let for a value that will change.
let score = 10;
score = 15; // allowed, because score is a let
console.log(country);
console.log(score);
OUTPUT   TERMINAL
➜ webbo3 node app.js
Nigeria
15
const cannot be reassigned

If you try to change a const, JavaScript stops with an error. That is the point: it protects a value you said should never change.

What this code doesTrying to reassign a const is an error, on purpose.
app.js
1
2
const price = 1500;
price = 2000; // not allowed: price is a const
OUTPUT   TERMINAL
➜ webbo3 node app.js
TypeError: Assignment to constant variable.

3. Naming Variables

A good name says what the value is, in plain words. A reader should understand your code without guessing.

  • Use clear words: firstName, totalPrice, isLoggedIn.
  • Start with a letter. A name cannot start with a number.
  • No spaces. Join words with capitals (camelCase), like userAge.
  • Names are case sensitive: age and Age are two different variables.
Reassignment is not redeclaration

Changing a value is score = 15 (no let). Writing let score again tries to make a second box with the same name, which is an error. Make the box once, then just change its value.

Today’s Mini Project

👤 Student Profile

Create variables for a student’s name, age, course and country, then print a tidy profile. This is the first time your program holds several pieces of information at once.

What this code doesFour facts, four variables. The + joins text together so each line reads nicely. Change the values to your own.
app.js
1
2
3
4
5
6
7
8
9
10
11
// Each fact gets its own clearly named box.
const name = "Ada Obi";
const age = 22;
const course = "JavaScript Simplified";
const country = "Nigeria";

// Read them back to show the profile.
console.log("Name: " + name);
console.log("Age: " + age);
console.log("Course: " + course);
console.log("Country: " + country);
OUTPUT   TERMINAL
➜ webbo3 node app.js
Name: Ada Obi
Age: 22
Course: JavaScript Simplified
Country: Nigeria
Challenge (15 minutes)

Add a variable for the year you plan to finish, then print one more line: "Finishing in: " + year. Expected: a five-line profile that includes your target year.

Try This

Make a let called mood, set it to "excited", print it, then on the next line set it to "focused" and print it again.

Expected: two lines, "excited" then "focused". The same box held two values in turn.

Picture This

Your program used to only say fixed words. Now it has memory: little labelled jars holding your name, your age, your country. Every app you will ever build is, at heart, values in boxes being read and changed.

Checkpoint

What is the difference between let and const?

let makes a variable whose value can change later. const makes one whose value cannot change once set. Use const by default, and let only when the value genuinely needs to change.

What does the = sign do in let age = 22?

It assigns: it puts the value 22 into the box named age. It does not mean "equals" in the maths sense. It means "store this value here".

Why use clear variable names?

Because code is read far more than it is written. A name like totalPrice tells the reader what the value is without guessing, so the code explains itself.

Tick Before You Move On

  • ☐ I can make a variable and read its value back
  • ☐ I can say when to use const and when to use let
  • ☐ I can name variables clearly and know a name cannot start with a number
  • ☐ I built the Student Profile

Quick Recap

A variable is a named box that holds a value. let makes a box whose value can change; const makes one that cannot. Use const by default; it protects values that should stay the same. Good names say what the value is, so the code reads clearly.

Next: Day 3. You have stored names, ages and prices without noticing they are different kinds of value. Tomorrow you meet data types: text, numbers, true/false and the rest.

Learn this with a real instructor

Free lessons take you a long way on your own. The full programmes add live classes, assignments, Skill Points and a certificate — built for African students.