By the end, you can store values in JavaScript variables using let and const, assign and reassign values, and choose the right declaration for data that changes or stays fixed.
What it is
A JavaScript variable is a named binding that connects an identifier, such as name, to a value, such as "Ada". A useful mental model is a label attached to a value: the label can be moved to a new value when you reassign it, but the label itself is not the value.
Declaration creates the binding. Assignment stores a value in it. For primitive values like strings and numbers, the binding holds the value. For objects and arrays, the binding holds a reference to the object. Related terms include identifier, binding, scope, reassignment, and temporal dead zone, the period before a let or const declaration is reached.
Why it matters
- Variables store user input, such as a name entered in a form.
- They track changing state, such as a score, timer, or selected item.
- They make code readable by giving values meaningful names.
- They let you pass data between functions and reuse calculations.
- Using
consthelps prevent accidental reassignment.
Syntax or steps
- Choose a declaration keyword:
constif the binding will not be reassigned,letif it will change. - Write a descriptive identifier, such as
userNameortotalPrice. - Use
=to assign an initial value. - Reassign only variables declared with
let.
const name = "Ada";
let age = 36;
age = age + 1;
Example
const greeting = "Hello";
let name = "Ada";
let message = greeting + ", " + name + "!";
console.log(message);
name = "Grace";
message = greeting + ", " + name + "!";
console.log(message);
const greeting = "Hello";creates a binding that cannot be reassigned.let name = "Ada";creates a binding that can be reassigned.let message = greeting + ", " + name + "!";evaluates the right side first, then stores the resulting string inmessage.name = "Grace";changes the value associated withname.- The second
console.logshows the updated message.
Common mistakes
- Using
constfor a value that changes. This causes aTypeError. Useletwhen reassignment is needed. - Forgetting to declare a variable. This can create accidental globals in non-strict code or throw a
ReferenceErrorin strict mode. Always declare withletorconst. - Using
varby habit.varhas function scope and hoisting behavior that can cause surprises. Preferletandconst. - Assuming
constfreezes objects.constprevents reassignment, not mutation. You can still change object properties unless you freeze the object.
When to use it
Variables are the basic way to store data. The closest choice is which declaration keyword to use.
| Keyword | Reassignment | Scope | Best for |
|---|---|---|---|
const | No | Block | Values that should not be reassigned |
let | Yes | Block | Values that change over time |
var | Yes | Function | Legacy code only |
A good default is const. Switch to let only when the binding must change.
Practice
Guided exercise: create a constant app name, a variable for completed tasks, increase it, and log both values.
const appName = "Study Tracker";
let tasksCompleted = 0;
tasksCompleted = tasksCompleted + 3;
console.log(appName, tasksCompleted);
Expected output: Study Tracker 3.
Challenge: store a price in a const, store a discount in a let, calculate the final price, and log it. Hint: use let only if the discount changes.
Quick check
Question: Which declaration should you use for a variable whose value will change later, and why?
Answer: Use let, because it allows reassignment within its block scope. const creates a binding that cannot be reassigned.
Summary
JavaScript variables are named bindings that store values or references. Assignment evaluates a value and attaches it to a binding, while reassignment changes that binding.
Prefer const for fixed bindings and let for changing ones, and avoid var in modern code.