Documentation

Student-friendly HTML, CSS, and JavaScript reference with examples

Sections

Starting Counters and Sums

When adding numbers or counting in a loop, start with 0 or an empty array.

Starting a Sum at 0

let total = 0;
for (const num of [1, 2, 3]) {
  total = total + num;
}
// total is now 6

Starting a Counter at 0

let count = 0;
for (const item of array) {
  if (condition) {
    count = count + 1;
  }
}

Starting with an Empty Array

const result = [];
for (const item of array) {
  if (condition) {
    result.push(item);
  }
}

Why Start at 0?

  • For sums: 0 + 1 = 1 (doesn’t change the first number)
  • For counts: Start from nothing and add 1 each time
  • For arrays: Start empty and add items one by one