continue

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

continue 文は、現在のループまたはラベル付きループの現在反復処理中の文の実行を終了し、次の反復処理としてループの実行を続けます。

試してみましょう

let text = "";

for (let i = 0; i < 10; i++) {
  if (i === 3) {
    continue;
  }
  text = text + i;
}

console.log(text);
// Expected output: "012456789"

構文

js
continue;
continue label;
label 省略可

その文のラベルに関連付けられた識別子。

解説

break 文と対照的に、 continue はループの実行を完全には終了しません。代わりに、

  • while または do...while ループでは、条件式にジャンプします。
  • for ループでは、更新式にジャンプします。
  • for...infor...offor await...of ループでは、次の反復処理にジャンプします。

continue 文には任意でラベルを含めることができ、現在のループの代わりにラベル付きループ文の次の反復処理へジャンプすることができます。この場合、continue 文は、ラベル付き文の中にある必要があります。

continue 文は、その後にラベルがあるかどうかに関わらず、スクリプト、モジュール、関数の本体、静的初期化ブロックのそれぞれ最上位で使用することはできません。その関数やクラスがさらにループの中に含まれていた場合でもです。

while における continue の使用

次の例では、 while ループで i の値が 3 であるときに実行される continue 文を持つものを示しています。よって、 n は 1、3、7、12 の値をとります。

js
let i = 0;
let n = 0;

while (i < 5) {
  i++;

  if (i === 3) {
    continue;
  }

  n += i;
}

ラベル付き continue の使用

次の例では、checkIAndJ とラベル付けされた文が、checkJ とラベル付けされた文を含んでいます。もし continue と遭遇したなら、プログラムは checkJ 文の先頭から継続します。continue と遭遇するたびに、checkJ の条件が false を返すまで、checkJ は再度反復処理します。false が返されたら、checkIAndJ 文の残りが完遂されます。

もし continue がラベル checkIAndJ を持っていたなら、プログラムは checkIAndJ 文の先頭から継続します。

js
let i = 0;
let j = 8;

checkIAndJ: while (i < 4) {
  console.log(`i: ${i}`);
  i += 1;

  checkJ: while (j > 4) {
    console.log(`j: ${j}`);
    j -= 1;

    if (j % 2 === 0) continue checkJ;
    console.log(`${j} is odd.`);
  }
  console.log(`i = ${i}`);
  console.log(`j = ${j}`);
}

結果:

i: 0

// start checkj
j: 8
7 is odd.
j: 7
j: 6
5 is odd.
j: 5
// end checkj

i = 1
j = 4

i: 1
i = 2
j = 4

i: 2
i = 3
j = 4

i: 3
i = 4
j = 4

構文違反の continue 文

continue は関数の境界をまたがるループ内で使用することはできません。

js
for (let i = 0; i < 10; i++) {
  (() => {
    continue; // SyntaxError: Illegal continue statement: no surrounding iteration statement
  })();
}

ラベルを参照する場合は、ラベル付きの文がその continue 文を含んでいなければなりません。

js
label: for (let i = 0; i < 10; i++) {
  console.log(i);
}

for (let i = 0; i < 10; i++) {
  continue label; // SyntaxError: Undefined label 'label'
}

ラベル付きの文はループでなければなりません。

js
label: {
  for (let i = 0; i < 10; i++) {
    continue label; // SyntaxError: Illegal continue statement: 'label' does not denote an iteration statement
  }
}

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-continue-statement

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
continue

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報