Revision 235903 of if...else

  • Revision slug: JavaScript/Reference/Statements/if...else
  • Revision title: if...else
  • Revision id: 235903
  • Created:
  • Creator: Lonnen
  • Is current revision? No
  • Comment added missing {; 1 words added

Revision Content

 

Summary

Executes a statement if a specified condition is true. If the condition is false, another statement can be executed.

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262

Syntax

if (condition)
   statement1
[else
   statement2]

Parameters

condition 
An expression that evaluates to true or false.
statement1 
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.
statement2 
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.

Description

Multiple if...else statements can be nested to create an else if clause:

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

Example: Using if...else

if (cipher_char == from_char) {
   result = result + to_char;
   x++;
} else {
   result = result + clear_char;

Example: 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 */
}

 

 

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/if...else", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/if...else", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/if...else", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/if...else" } ) }}

Revision Source

<p> </p>
<h3 name="Summary">Summary</h3>
<p>Executes a statement if a specified condition is true. If the condition is false, another statement can be executed.</p>
<table class="fullwidth-table"> <tbody> <tr> <td class="header" colspan="2">Statement</td> </tr> <tr> <td>Implemented in:</td> <td>JavaScript 1.0, NES 2.0</td> </tr> <tr> <td>ECMA Version:</td> <td>ECMA-262</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">if (<em>condition</em>)
   <em>statement1</em>
[else
   <em>statement2</em>]
</pre>
<h3 name="Parameters">Parameters</h3>
<dl> <dt><code>condition</code> </dt> <dd>An expression that evaluates to true or false.</dd>
</dl>
<dl> <dt><code>statement1</code> </dt> <dd>Statement that is executed if <code>condition</code> evaluates to true. Can be any statement, including further nested <code>if</code> statements. To execute multiple statements, use a <a href="/en/JavaScript/Reference/Statements/block" title="en/JavaScript/Reference/Statements/block">block</a> statement ({ ... }) to group those statements.</dd>
</dl>
<dl> <dt><code>statement2</code> </dt> <dd>Statement that is executed if <code>condition</code> evaluates to false and the <code>else</code> clause exists. Can be any statement, including block statements and further nested <code>if</code> statements.</dd>
</dl>
<h3 name="Description">Description</h3>
<p>Multiple <code>if...else</code> statements can be nested to create an <code>else if</code> clause:</p>
<pre class="eval">if (<em>condition1</em>)
   <em>statement1</em>
else if (<em>condition2</em>)
   <em>statement2</em>
else if (<em>condition3</em>)
   <em>statement3</em>
...
else
   <em>statementN</em>
</pre>
<p>To see how this works, this is how it would look like if the nesting were properly indented:</p>
<pre class="eval">if (<em>condition1</em>)
   <em>statement1</em>
else
   if (<em>condition2</em>)
      <em>statement2</em>
   else
      if (<em>condition3</em>)
...
</pre>
<p>To execute multiple statements within a clause, use a block statement (<code>{ ... }</code>) to group those statements. In general, it is a good practice to always use block statements, especially in code involving nested <code>if</code> statements:</p>
<pre class="eval">if (<em>condition</em>) {
   <em>statements1</em>
} else {
   <em>statements2</em>
}
</pre>
<p>Do not confuse the primitive boolean values <code>true</code> and <code>false</code> with the true and false values of the <a href="/en/JavaScript/Reference/Global_Objects/Boolean" title="en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a> object. Any value that is not <code>undefined</code>, <code>null</code>, <code>0</code>, <code>NaN</code>, or the empty string (<code>""</code>), and any object, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example:</p>
<pre class="eval">var b = new Boolean(false);
if (b) // this condition evaluates to true
</pre>
<h3 name="Examples">Examples</h3>
<h4 name="Example:_Using_if...else">Example: Using <code>if...else</code></h4>
<pre class="eval">if (cipher_char == from_char) {
   result = result + to_char;
   x++;
} else {
   result = result + clear_char;
</pre>
<h4 name="Example:_Assignment_within_the_conditional_expression">Example: Assignment within the conditional expression</h4>
<p>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:</p>
<pre class="eval">if (x = y) {
   /* do the right thing */
}
</pre>
<p>If you need to use an assignment in a conditional expression, a common practice is to put additional parentheses around the assignment. For example:</p>
<pre class="eval">if ((x = y)) {
   /* do the right thing */
}
</pre>
<p> </p>
<p> </p>

<p>{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/if...else", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/if...else", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/if...else", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/if...else" } ) }}</p>
Revert to this revision