Variables store values that later expressions can reuse.

Variables

variables.js
const unitPrice = 12;
const quantity = 3;
const total = unitPrice * quantity;

console.log("unit=" + unitPrice);
console.log("total=" + total);

Follow the Total

  1. unitPrice starts at 12.
  2. quantity starts at 3.
  3. total = unitPrice * quantity multiplies 12 * 3.
  4. total becomes 36.
  5. The program prints unit=12 and total=36. | unitPrice | quantity | total | | --- | --- | --- | | 8 | 3 | 24 | | 12 | 3 | 36 | | 20 | 3 | 60 |
const Use `const` when a local binding should not be reassigned.

Exercise: variables.js

Reproduce unit=12 and total=36, then try unitPrice 8 and 20 and predict each total before running it.