「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と評価された場合のみ実行されます。
次の例は、この構文の使い方を示しています:
123456789let 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."); }
すべて明確でしたか?
フィードバックありがとうございます!
セクション 3. 章 9
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 3. 章 9