Variables: Let and Const
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.constdoesn'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.
varis the older way (before 2015). Read it, but writeletandconst.
Declaring Variables
Syntax
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 undefinedDeclaring creates the name. Assigning (=) gives it a value. Initializing is doing both at once.
Example
const studentName = "Navika Anamaneni";
let balanceDue = 120;
console.log(studentName, "owes", balanceDue);
balanceDue = balanceDue - 50; // a payment of $50
console.log(studentName, "owes", balanceDue);Navika Anamaneni owes 120
Navika Anamaneni owes 70Explanation: 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 fee = 120;
fee = 150;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 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);{ 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
| Rule | Allowed | Not allowed |
|---|---|---|
Letters, digits, _ and $ | fee2, _total, $price | late-fee, fee% |
| Can't start with a digit | term1 | 1term |
| Can't be a reserved word | className, newFee | class, new, const |
| Case-sensitive | fee and Fee are different |
Conventions that every JavaScript team follows:
| Kind | Convention | Example |
|---|---|---|
| Variables and functions | camelCase | balanceDue, lateFee |
| Classes | PascalCase | FeeTracker |
| Fixed configuration values | UPPER_SNAKE_CASE | MAX_LATE_DAYS |
| Booleans | Start with is, has, can | isPaid, 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.
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}`);var studentName = "Srihitha Anamaneni";
var termFee = 450;
var paid = 0;
paid = paid + 200;
paid = paid + 150;
var balance = termFee - paid;
console.log("" + studentName + ": paid $" + paid + ", balance $" + balance);Srihitha Anamaneni: paid $350, balance $100Why 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:
| Behavior | var | let / const |
|---|---|---|
| Scope | The whole function, ignoring { } blocks | Only the block it's in |
| Using it before the declaration line | Gives undefined silently | Throws a ReferenceError |
| Declaring the same name twice | Allowed (silently overwrites) | SyntaxError |
if (true) {
var oldStyle = "leaks out of the block";
let newStyle = "stays inside";
}
console.log(oldStyle);
console.log(typeof newStyle);leaks out of the block
undefinedExplanation: 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:
console.log(total);
let total = 10;ReferenceError: Cannot access 'total' before initializationDeclaring 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
const settings = { currency: "USD" };
settings.currency = "EUR";
let count = 1;
count += 1;
console.log(settings.currency, count);EUR 2settings 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
const tuition = 120;
const books = 85;
const lab = 45;
let total = 0;
total += tuition;
total += books;
total += lab;
console.log(`Total: $${total}`);Total: $250Key Takeaways
| Keyword | Reassign | Scope | Before declaration |
|---|---|---|---|
const | No | Block | ReferenceError |
let | Yes | Block | ReferenceError |
var (older) | Yes | Function | undefined |
Was this tutorial helpful?