bhylu.com

Advertisements

JS Break

Break leaves the nearest loop. It is useful when further work cannot change the answer.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

for (const size of [2, 4, 8, 16]) {
  if (size >= 8) {
    console.log(size);
    break;
  }
}

What happens

Only the first suitable size, 8, is printed.