Academy
ProgrammingJS for BeginnersVariables & Memory

Variables & Memory

The Problem With Lesson 1

Every program you ran in the previous lesson had a flaw: it forgot everything the moment it finished.

console.log(10 + 20) computes 30 and prints it. But that 30 disappears immediately. The next line has no idea it ever existed. For a calculator that prints one answer and dies, that's fine. For anything real — a game that tracks your score, a form that remembers your name, a chart that updates live — you need the computer to hold on to values.

That's what a variable is.

A Box With a Label

Think of your computer's memory as a warehouse full of small boxes. Each box can hold one value. A variable is simply a label you stick on a box so you can find it again later.

let score = 0;

This instruction does three things:

  • Creates a new box in memory
  • Puts the value 0 inside it
  • Sticks the label score on it

From this point on, anywhere you write score, the computer opens that box and uses whatever is inside.

Read line 4 carefully: score = score + 5.

The right side is evaluated first. The computer opens the score box, finds 10, adds 5, gets 15. Then it puts 15 back into the same box, replacing the old value. The label doesn't change — only the contents do.

This is called reassignment and it's one of the most fundamental operations in all of programming.

The Word let

let is a keyword — a word that JavaScript reserves for itself with a fixed meaning. It says: "Create a new variable."

You only write let once, when you create the variable. After that, you refer to it by name alone.

let city = "Kolkata"; // create
city = "Mumbai"; // reassign — no `let` here

Some Boxes Should Never Change

Not every value needs to change. The speed of light doesn't change. The number of days in a week doesn't change. Pi doesn't change.

For these, JavaScript gives you a different keyword: const.

Run this. The first console.log works fine. Then the program crashes with a TypeError.

const creates a box that is sealed shut the moment you fill it. You can look inside. You cannot replace the contents. If you try, JavaScript stops you immediately.

This is not a limitation — it is a safety feature. When you mark something const, you are making a promise to yourself and to anyone reading your code: "This value will never change here." That promise makes programs easier to understand and bugs easier to find.

Rule of thumb: Use const by default. Switch to let only when you know the value needs to change.

Naming Variables

You are free to name your variables almost anything. But names carry meaning, and meaning matters.

Both programs do identical calculations. But the second one tells a story. Three months from now, you will understand the second instantly and spend five minutes puzzling over the first.

A few naming rules JavaScript enforces:

  • Names can contain letters, digits, _, and $
  • Names cannot start with a digit
  • Names are case-sensitive: score, Score, and SCORE are three different variables

And one rule that is not enforced but should be:

  • Name variables for what they contain, not for how they are used

What Happens If You Forget to Declare?

You get a ReferenceError. The computer looks through every labelled box in memory, finds no box labelled temperature, and stops with an error.

This is the computer being your friend. It is telling you clearly: "You asked for something that doesn't exist." An error here is infinitely better than a program that silently uses the wrong value and produces wrong results without complaint.

The Third Keyword: var

You will encounter a third keyword in older JavaScript code: var. It was the original way to declare variables, and it still works. But it has some confusing behaviours that let and const were designed to fix.

For now: do not use var. Use const and let. When you understand scope deeply (a later lesson), you will understand exactly why var exists and when, if ever, to reach for it.

What You Have Learned

Variables are how a program builds memory across time. Without them, every instruction is an island.

The key ideas:

  • let creates a variable that can be reassigned
  • const creates a variable that is sealed — it can never be reassigned
  • A name is a label on a box; reassignment replaces the contents, not the label
  • Undeclared variables cause ReferenceError — the computer's way of saying "this doesn't exist"
  • Name things clearly; you are writing for the next human reader, who might be you

In the next lesson, we go deeper into the kinds of values that can live inside these boxes. Numbers, text, true/false, and the two strange ones — null and undefined — that represent the absence of a value.