JSE Certified Entry-Level JavaScript Programmer study materials
JSE Certified Entry-Level JavaScript Programmer study resources

JSE Certified Entry-Level JavaScript Programmer: Control Flow and Loops

JSE Certified Entry-Level JavaScript Programmer · Work through your exam, one topic at a time.

All guide sections

Control Flow and Loops

What this domain covers

Branching and iteration: if/else, switch, the loop forms, and the break/continue/label machinery. Items usually ask how many times a body runs or what a leaked loop variable holds afterwards.

Branching

if evaluates its condition with ToBoolean. switch compares with strict equality, so switch ('1') will not match case 1:. Without break, execution falls through into the following cases; default can appear anywhere but is only reached when no case matched (or by fall-through from an earlier case).

Loop forms

  • for (init; test; update) - update runs after each iteration, before the next test.
  • while tests first; do...while runs the body at least once.
  • for...of iterates values of iterables (arrays, strings, Map, Set).
  • for...in iterates enumerable keys, including inherited ones - the wrong tool for arrays, and it yields strings.

break leaves the loop; continue jumps to the update/test. A label lets break outer; exit nested loops.

Worked example

for (var i = 0; i < 3; i++) {}
console.log(i);
const arr = ['a', 'b'];
for (const k in arr) console.log(typeof k);

Reasoning: var is function-scoped, so i survives the loop and holds 3. for...in yields property names, which are strings, so the log prints 'string' twice. Replacing var with let would make i block-scoped and console.log(i) a ReferenceError.

Traps

  • Omitting break in switch and running unintended cases.
  • Off-by-one: i <= arr.length reads one element past the end.
  • for...in on arrays returning index strings rather than numbers.
  • A const loop variable in a classic for header fails on i++.
  • forEach cannot be exited with break; use some, find, or a real loop.