Skip to content

Variables

Learn what variables are, how to create them, and how they store data your program can reuse.

What you'll learn

  • What a variable is and why we use them
  • How to declare and assign a variable
  • The difference between let, const, and var
  • Rules and conventions for naming variables

Concept

Variables

A variable is a named container for a piece of data. Think of it as a labeled box: you put a value inside, write a name on the box, and later you can look inside by using the name. Without variables, you would have to repeat the same value everywhere — and if it changed, you'd have to find and update every copy.

Declaring and Assigning

In JavaScript you create a variable in two steps (often combined):

let score;          // declare: create the box
score = 0;          // assign: put a value inside
let lives = 3;      // declare + assign in one line

let, const, and var

  • const — the value cannot be reassigned. Use this by default; it prevents accidental changes.
  • let — the value can be reassigned. Use it when the value must change (a counter, a score).
  • var — an older keyword with confusing scoping rules. Avoid it in modern code.

Naming Rules and Conventions

Names must start with a letter, $, or _ — not a number. They cannot contain spaces. By convention, JavaScript uses camelCase: firstName, totalPrice. Choose names that describe the meaning, not the type: userAge is clear; x is not.

Good names make code self-documenting. When you read const maxRetries = 3, you understand the intent without a comment.

Example

              const productName = "Notebook";   // const: won't change
let quantity = 2;               // let: will change
const price = 4.99;

let total = quantity * price;   // 9.98
console.log(productName, "x", quantity, "=", total);

quantity = 5;                   // reassign let
total = quantity * price;       // 24.95
console.log(productName, "x", quantity, "=", total);

// productName = "Pen";  // ❌ Error: const cannot be reassigned
            

const creates a fixed binding; let creates a changeable one. Notice we recompute total after changing quantity — the variable does not update automatically.

Try it

  • Case Converter

    Variables hold the input text and the converted result. Try naming your own test strings.

Common mistakes

The mistake

Using let for everything, even values that never change.

The fix

Default to const. Switch to let only when you actually need to reassign. This catches accidental mutations at build time.

The mistake

Using vague names like x, data, or temp.

The fix

Name variables by their meaning: invoiceTotal, currentUser, retryCount. Future you will thank present you.

Related Cheatsheets

Practice Variables

2 exercises

Practice Variables

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors