Lesson contents

Keep a running total

Start at zero and add each value as you reach it.

Simple example

Start2 + 4
Result6
lesson.ts
let total: number = 0;
total += 2;
total += 4;
console.log(total);
Output
6
Advertisements

Everyday examples

Three classes donate 5, 2 and 4 books. How many books can the library put on its shelves?

Start5 + 2 + 4
Result11
lesson.ts
const books: number[] = [5, 2, 4];
let total: number = 0;
let i: number = 0;
while (i < 3) {
    total += books[i];
    i += 1;
}
console.log(total);
Output
11
Run the example
Terminal
tsc --target ES2020 lesson.ts
node lesson.js
Official documentation