if...else

翻譯不完整。 請幫助我們翻譯這篇文章!

當條件成立的時候會執行 if 陳述式裡的程式,而不成立時則執行另外一個陳述式。

語法

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 falseundefined, 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

Specification Status Comment
ECMAScript (ECMA-262)
The definition of 'if statement' in that specification.
Living Standard
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'if statement' in that specification.
Standard
ECMAScript 5.1 (ECMA-262)
The definition of 'if statement' in that specification.
Standard
ECMAScript 3rd Edition (ECMA-262)
The definition of 'if statement' in that specification.
Standard
ECMAScript 1st Edition (ECMA-262)
The definition of 'if statement' in that specification.
Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
if...elseChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100

Legend

Full support  
Full support

See also