Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 「else if」句 | 条件文
JavaScript入門

book「else if」句

メニューを表示するにはスワイプしてください

else節に加えて、条件分岐ではelse if節もサポートされており、最初のif条件がfalseの場合に代替条件を定義するために使用できます。

一般的な構文は次のとおりです:

if(expression) {
  // Code … (executed if the expression is true)
} else if(expression) {
  // Fallback Code … 
  //(executed if the previous condition is false, and this one is true)
}

一般的な構文で示されているように、else if節はブール式を受け取り、直前の条件がfalseになったときに評価されます。

複数のelse if節を連結して、if-else ifチェーンを作成できます:

if(expression) {
  // … (executed if the first condition is true)
} else if(expression) {
  // … (executed if the first condition is false and this is true)
} else if(expression) {
  // … (executed if previous conditions are false and this is true)
} else {
  // … (executed if all previous conditions are false)
}

上記のコードのように、最後にelse節を追加することもできます。このブロックは、すべての前の条件がfalseと評価された場合のみ実行されます。

次の例は、この構文の使い方を示しています:

123456789
let number = 50; if (number < 20) { console.log("The number is less than 20."); } else if (number === 20) { console.log("The number is exactly 20."); } else { console.log("The number is greater than 20."); }
copy
question mark

if-else文におけるelse if節の目的は何ですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 3.  9

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 3.  9
some-alt