Variables: Let and Const

Updated · 7 min read
easyES2026javascriptvariablesletconstvarinterview

A variable is a name that refers to a value, so a program can store it, use it later and change it. JavaScript declares variables with const (the name always points to the same value) and let (the name can be pointed at a new value). The Fee Tracker needs to remember a student's name and the balance they still owe: that is exactly what variables are for.

In One Minute

  • const name = value; declares a variable that can't be reassigned. Use it by default.
  • let name = value; declares a variable that can be reassigned. Use it when the value must change, such as a running total.
  • const doesn't freeze objects: the name can't move, but the object's contents can change.
  • Names are case-sensitive, can't start with a digit, and use camelCase by convention.
  • var is the older way (before 2015). Read it, but write let and const.

Declaring Variables

Syntax

syntax.js
const name = value;   // must be given a value now; can't be reassigned
let name = value;     // can be reassigned later
let name;             // declared without a value: it holds undefined

Declaring creates the name. Assigning (=) gives it a value. Initializing is doing both at once.

Example

declare.js▶ Try it
const studentName = "Navika Anamaneni";
let balanceDue = 120;
 
console.log(studentName, "owes", balanceDue);
 
balanceDue = balanceDue - 50;   // a payment of $50
console.log(studentName, "owes", balanceDue);
Output
Navika Anamaneni owes 120
Navika Anamaneni owes 70

Explanation: studentName never changes, so it is a const. balanceDue changes after a payment, so it is a let. The second assignment reads the old value (120), subtracts 50, and stores 70 under the same name.

Reassigning a const Fails

const-error.js▶ Try it
const fee = 120;
fee = 150;
Output
TypeError: Assignment to constant variable.

That error is the point of const: when you read const fee, you know that nothing later in the code points fee at another value. Fewer things can change, so there are fewer things to keep in your head.

const Doesn't Mean Frozen

const protects the binding (the link between the name and the value), not the value itself. When the value is an object or an array, you can still change what is inside it:

const-object.js▶ Try it
const student = { name: "Advitha", grade: 9 };
student.grade = 10;            // allowed: changing a property
console.log(student);
 
const fees = [120, 80];
fees.push(45);                 // allowed: changing the array
console.log(fees);
Output
{ name: 'Advitha', grade: 10 }
[120, 80, 45]

What const stops is student = { ... }, pointing the name at a different object. To make the object itself unchangeable, use Object.freeze, covered in the lesson on protecting objects.

Naming Rules and Conventions

RuleAllowedNot allowed
Letters, digits, _ and $fee2, _total, $pricelate-fee, fee%
Can't start with a digitterm11term
Can't be a reserved wordclassName, newFeeclass, new, const
Case-sensitivefee and Fee are different

Conventions that every JavaScript team follows:

KindConventionExample
Variables and functionscamelCasebalanceDue, lateFee
ClassesPascalCaseFeeTracker
Fixed configuration valuesUPPER_SNAKE_CASEMAX_LATE_DAYS
BooleansStart with is, has, canisPaid, hasDiscount

In Practice

The Fee Tracker's opening lines, written the older way and the modern way. Both run, but the modern version tells the reader which values change.

fees.js▶ Try it
const studentName = "Srihitha Anamaneni";
const termFee = 450;
let paid = 0;
 
paid = paid + 200;
paid = paid + 150;
 
const balance = termFee - paid;
console.log(`${studentName}: paid $${paid}, balance $${balance}`);
Output
Srihitha Anamaneni: paid $350, balance $100

Why var Was Replaced

var still works, and you'll see it in older code, tutorials and some platform scripts. It was replaced because of three surprises:

Behaviorvarlet / const
ScopeThe whole function, ignoring { } blocksOnly the block it's in
Using it before the declaration lineGives undefined silentlyThrows a ReferenceError
Declaring the same name twiceAllowed (silently overwrites)SyntaxError
var-block.js▶ Try it
if (true) {
  var oldStyle = "leaks out of the block";
  let newStyle = "stays inside";
}
console.log(oldStyle);
console.log(typeof newStyle);
Output
leaks out of the block
undefined

Explanation: var oldStyle belongs to the surrounding scope, so it is still visible after the if block ends. let newStyle disappears when the block ends; typeof on a name that doesn't exist gives "undefined" instead of an error. The scope chapter explains scope and hoisting in full.

Common Mistakes

Using a variable before declaring it. With let and const, that is an error, not a silent undefined:

before-declare.js▶ Try it
console.log(total);
let total = 10;
Output
ReferenceError: Cannot access 'total' before initialization

Declaring a const without a value. const fee; is a SyntaxError: Missing initializer in const declaration. A const must get its value immediately.

Thinking const objects can't change. As shown above, their contents can. Use const to stop reassignment, Object.freeze to stop changes.

Forgetting let or const entirely. total = 10 without a declaration creates an accidental global variable in old-style scripts, and throws ReferenceError in strict mode and modules. Always declare.

Interview Corner

What is the difference between let, const and var?

let and const are block-scoped and can't be used before their declaration line (the temporal dead zone); const also can't be reassigned and must be initialized. var is function-scoped, is hoisted with the value undefined, and can be redeclared. Modern code uses const by default and let when reassignment is needed.

Can you change a const array?

Yes, its contents. const prevents rebinding the name, not mutating the value: push, pop and index assignment all work on a const array. Reassigning (arr = []) throws TypeError: Assignment to constant variable. Use Object.freeze to prevent mutation (shallowly).

What does this print?

Show solution
puzzle.js▶ Try it
const settings = { currency: "USD" };
settings.currency = "EUR";
let count = 1;
count += 1;
console.log(settings.currency, count);
Output
EUR 2

settings is never reassigned, only changed inside, so const allows it. count is reassigned, which let allows.

Practice

Challenge: Store three fees (120, 85 and 45) in const variables, keep a running total in a let that starts at 0, add each fee to it, and print Total: $250.

Show solution
total.js▶ Try it
const tuition = 120;
const books = 85;
const lab = 45;
 
let total = 0;
total += tuition;
total += books;
total += lab;
 
console.log(`Total: $${total}`);
Output
Total: $250

Quick check: Variables

4 questions
1Which declaration should you use by default?
2What happens with const scores = [1, 2]; scores.push(3);?
3Which name is not allowed?
4What does console.log(x); let x = 5; do?

Key Takeaways

KeywordReassignScopeBefore declaration
constNoBlockReferenceError
letYesBlockReferenceError
var (older)YesFunctionundefined

Was this tutorial helpful?

Lesson 6 of 139