ES6 - if 語句



‘if…else’ 結構在執行程式碼塊之前會評估條件。

以下是語法。

if(boolean_expression) {
   // statement(s) will execute if the Boolean expression is true
}

如果布林表示式計算結果為真,則將執行 if 語句內的程式碼塊。如果布林表示式計算結果為假,則將執行 if 語句結束後的第一組程式碼(右花括號之後)。

流程圖

If Statement

示例

var num = 5
if (num>0) {
   console.log("number is positive")
}

以上程式碼成功執行後將顯示以下輸出。

number is positive

以上示例將列印“number is positive”,因為 if 塊指定的條件為真。

廣告
© . All rights reserved.