if/else, switch, the ternary, five kinds of loop, and break/continue with labels.
Conditionals
const grade = (score) => {
if (score >= 90) return "A";
else if (score >= 75) return "B";
else return "C";
};
console.log(grade(95), grade(80), grade(10));
const age = 20;
console.log(age >= 18 ? "adult" : "minor");A B C
adultswitch uses strict equality and falls through
function kind(day) {
switch (day) {
case "sat":
case "sun":
return "weekend";
case 1:
return "number one";
default:
return "weekday";
}
}
console.log(kind("sun"), kind("mon"), kind(1), kind("1"));
let log = [];
switch (2) {
case 1: log.push(1);
case 2: log.push(2); // no break → falls through
case 3: log.push(3);
break;
case 4: log.push(4);
}
console.log(log);weekend weekday number one weekday
[ 2, 3 ]Loops
| Loop | Iterates over | Use for |
|---|---|---|
for (init; cond; step) | a counter | index-based work, early exit |
while (cond) | until the condition fails | unknown number of iterations |
do { } while (cond) | at least once | prompts / retries |
for (const x of iterable) | values of arrays, strings, maps, sets, generators | the default for arrays |
for (const k in object) | enumerable keys (incl. inherited) as strings | plain objects only |
arr.forEach(fn) | array elements | side effects; cannot break |
const arr = ["a", "b"];
arr.extra = "!";
for (const i in arr) console.log("in:", i, typeof i);
for (const v of arr) console.log("of:", v);
for (const [i, v] of arr.entries()) console.log(i, v);
for (const ch of "hi") console.log(ch);in: 0 string
in: 1 string
in: extra string
of: a
of: b
0 a
1 b
h
iwhile / do…while
let n = 1;
while (n < 100) n *= 3;
console.log(n);
let tries = 0;
do {
tries++;
} while (false);
console.log(tries);243
1break, continue and labels
for (let i = 0; i < 6; i++) {
if (i === 1) continue;
if (i === 4) break;
console.log(i);
}
outer: for (let r = 0; r < 3; r++) {
for (let c = 0; c < 3; c++) {
if (c === 1) continue outer;
if (r === 2) break outer;
console.log(`r${r}c${c}`);
}
}0
2
3
r0c0
r1c0What is the difference between for...in and for...of?
Can you break out of forEach?