Foundations
Variables
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
unitPricestarts at12.quantitystarts at3.total = unitPrice * quantitymultiplies12 * 3.totalbecomes36.- The program prints
unit=12andtotal=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.