Conditions and loops
if / else, switch, for, for…of, while, and the ternary operator.
Runs in Zerethon IDE · Node.js 22
Official JavaScript documentation ↗
main.js
for (let n = 1; n <= 4; n++) {
if (n % 2 === 0) {
console.log(n, 'even');
} else {
console.log(n, 'odd');
}
}
for (const fruit of ['apple', 'pear']) console.log(fruit);
const scores = { ada: 90, alan: 85 };
for (const [name, score] of Object.entries(scores)) {
console.log(`${name}: ${score >= 88 ? 'A' : 'B'}`);
}
let i = 0;
while (true) {
if (++i > 2) break;
console.log('i =', i);
}
Output
1 odd 2 even 3 odd 4 even apple pear ada: A alan: B i = 1 i = 2
Watch out: for…in walks an object's keys (as strings), not an array's values — use for…of for arrays.