Course progress0%
Course content

Module 1

Module 2

Module 3

Module 4

Module 5

Module 6

Module 7

Module 8

Module 9

Module 10

Module 11

Module 12

Module 13

Module 14

Module 15

Module 16

Module 17

Module 18

25 min

Variables and Constants

Distinguish a stable input from changing program state

By the end of this lesson

  • Distinguish a stable input from changing program state
  • Choose const or let intentionally

Think before choosing a keyword

A small shop starts the day with 12 available notebooks. Three are sold. The unit price stays 20. Which information changes during this run? Write the expected stock before reading further.

Stock becomes 9. The price does not change. This distinction is about the current program, not whether a price could ever change in real life.

A name bound to a value

const creates a binding that cannot be reassigned; let allows reassignment. A declaration introduces the name. Assignment gives it a value. Reassignment replaces that value. JavaScript evaluates the right side before assigning the result.

const unitPrice = 20;
let availableStock = 12;
const soldQuantity = 3;
availableStock = availableStock - soldQuantity;
console.assert(availableStock === 9, "Three notebooks should leave stock at nine");
console.log(unitPrice * soldQuantity);

The output is 60. availableStock changes from 12 to 9. Declaring everything with let would work, but would hide which values are intended to remain stable. Use const by default and let when a changing value is part of the algorithm.

var has different scoping and redeclaration behavior. You may encounter it in older programs; use const and let in this course so the lifetime of a name is easier to reason about. These keywords do not allocate a business record in a database.

Guided review

A colleague uses const availableStock = 12 and later assigns 9 to it. This raises an error. Changing to let is appropriate only if the algorithm really models changing stock. Alternatively, keep the original stock and calculate a separately named remaining stock.

Independent exercise: preserve the original information

The owner needs to report both opening and remaining stock. Rewrite the calculation using three constants: opening stock, sold quantity and remaining stock. Then change the sale to five notebooks. Predict both results before execution.

Correction

const openingStock = 12;
const soldQuantity = 5;
const remainingStock = openingStock - soldQuantity;
console.assert(openingStock === 12);
console.assert(remainingStock === 7);

Keeping the original value makes this report easier. Neither style is universally superior: choose according to what the next steps need. A sale exceeding stock is still an unresolved validation issue, not a reason to accept a negative stock silently.

Check your reasoning

Does const mean the business value can never change? No: it means this binding cannot be reassigned in this execution. Does = compare two values? No: it assigns. Explain these distinctions before continuing.

Lesson complete?

Your progress is saved on this device.