翻譯不完整。 請幫助我們翻譯這篇文章!
當條件成立的時候會執行 if 陳述式裡的程式,而不成立時則執行另外一個陳述式。
這個互動式的源碼被放在 GitHub 的 Repository 裡,如果您想對此互動式範例專案提出貢獻的話,請 clone https://github.com/mdn/interactive-examples 並送出 pull request。
語法
if (條件式) statement1 [else statement2]
條件式- 一個成立或不成立的運算式。
第一個陳述式(statement1)- 如果if中的條件(conditions)為真時執行陳述式(statements)。陳述式可以為任何內容,包含巢狀式(nested)的if陳述。當要執行多行的陳述式(statements)時,使用區塊(block)將所要執行的陳述式包覆。如果不需要執行任何動作時,則不撰寫任何陳述式(empty statement)。
第二個陳述式(statement2)
- 當件不成立時所執行的部份,當else被撰寫時才會被執行。可以是任何的陳述式,包含使用區塊(block)及巢狀(nested)的陳述。
Description
Multiple if...else statements can be nested to create an else if clause. Note that there is no elseif (in one word) keyword in JavaScript.
if (condition1) statement1 else if (condition2) statement2 else if (condition3) statement3 ... else statementN
To see how this works, this is how it would look if the nesting were properly indented:
if (condition1)
statement1
else
if (condition2)
statement2
else
if (condition3)
...
To execute multiple statements within a clause, use a block statement ({ ... }) to group those statements. In general, it is a good practice to always use block statements, especially in code involving nested if statements:
if (condition) {
statements1
} else {
statements2
}
Do not confuse the primitive boolean values true and false with truthiness or falsiness of the Boolean object. Any value that is not false, undefined, null, 0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, is considered truthy when used as the condition. For example:
var b = new Boolean(false); if (b) // this condition is truthy
Examples
Using if...else
if (cipher_char === from_char) {
result = result + to_char;
x++;
} else {
result = result + clear_char;
}
Using else if
Note that there is no elseif syntax in JavaScript. However, you can write it with a space between else and if:
if (x > 5) {
/* do the right thing */
} else if (x > 50) {
/* do the right thing */
} else {
/* do the right thing */
}
Assignment within the conditional expression
It is advisable to not use simple assignments in a conditional expression, because the assignment can be confused with equality when glancing over the code. For example, do not use the following code:
if (x = y) {
/* do the right thing */
}
If you need to use an assignment in a conditional expression, a common practice is to put additional parentheses around the assignment. For example:
if ((x = y)) {
/* do the right thing */
}
Specifications
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
if...else | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support 3 | Safari Full support 1 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support 0.1.100 |
Legend
- Full support
- Full support