Skip to content

Variable

In one line

A variable is a named container for storing a value that can be referenced and changed later.

In simple words

A variable is a labeled box that holds a value. You give it a name, store something in it, and later read or change what's inside. In JavaScript, you declare variables with let (changeable), const (unchangeable binding), or var (older, function-scoped).

Variables are how programs remember things. Without variables, every value would be anonymous and temporary. With variables, you can store user input, track game scores, hold API responses, and build up complex data structures.

Choose const by default — it prevents accidental reassignment. Use let when you know the value will change. Avoid var in modern code because its function-scoping and hoisting behavior can cause subtle bugs.

Example

javascript
let score = 0;
score = score + 10;
const playerName = "Ada";
console.log(playerName, score); // "Ada" 10

score is declared with let and reassigned. playerName is declared with const and cannot be reassigned.

Common confusions

  • Confused with: constant

    The difference: A variable (let) can be reassigned; a constant (const) cannot be reassigned, though if it holds an object, the object's properties can still change.

Related terms

Related Resources

Related Lessons

Decode error messages

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

View errors

← Back to glossary