The Wayback Machine - https://web.archive.org/web/20160412083300/https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Statements/if...else

Account creation on MDN is disabled while we upgrade our moderation mechanisms. If you see something that needs to be fixed, please file a bug: https://bugzilla.mozilla.org/form.doc and we'll handle it as soon as we can. Thanks for your patience!

if...else

Перевод не завершен. Пожалуйста, помогите перевести эту статью с английского.

Выражение if выполняет выражение, если указанное условие выполняется (верно). Если условие не выполняется (ложно), то другое выражение может быть выполнено.

Синтакс

if (условие)
   выражение1
[else
   выражение2]
условие
An expression that evaluates to true or false.
выражение1
Statement that is executed if condition evaluates to true. Can be any statement, including further nested if statements. To execute multiple statements, use a block statement ({ ... }) to group those statements, to execute no statements, use an empty statement.
выражение2
Statement that is executed if condition evaluates to false and the else clause exists. Can be any statement, including block statements and further nested if statements.

Описание

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 like 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 the true and false values of the Boolean object. Any value that is not undefined, null, 0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example:

var b = new Boolean(false);
if (b) // this condition evaluates to true

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) {

} else if (x > 50) {

} else {

}

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 2016 Draft (7th Edition, ECMA-262)
Определение 'if statement' в этой спецификации.
Черновик  
ECMAScript 2015 (6th Edition, ECMA-262)
Определение 'if statement' в этой спецификации.
Стандарт  
ECMAScript 5.1 (ECMA-262)
Определение 'if statement' в этой спецификации.
Стандарт  
ECMAScript 3rd Edition (ECMA-262)
Определение 'if statement' в этой спецификации.
Стандарт  
ECMAScript 1st Edition (ECMA-262)
Определение 'if statement' в этой спецификации.
Стандарт Initial definition

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Да) (Да) (Да) (Да) (Да)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Да) (Да) (Да) (Да) (Да) (Да)

See also

Метки документа и участники

 Внесли вклад в эту страницу: ultrinnan
 Обновлялась последний раз: ultrinnan,
HTTPS · web.archive.org
← Home