Student-friendly HTML, CSS, and JavaScript reference with examples
When adding numbers or counting in a loop, start with 0 or an empty array.
let total = 0;
for (const num of [1, 2, 3]) {
total = total + num;
}
// total is now 6
let count = 0;
for (const item of array) {
if (condition) {
count = count + 1;
}
}
const result = [];
for (const item of array) {
if (condition) {
result.push(item);
}
}