Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 567109 of if...else

  • Revision slug: Web/JavaScript/Reference/Statements/if...else
  • Revision title: if...else
  • Revision id: 567109
  • Created:
  • Creator: Delapouite
  • Is current revision? No
  • Comment fixed redirection link to block statement

Revision Content

Summary

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

Version Information

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

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Executes a statement if a specified condition is true. If the condition is false, another statement can be executed.</p>
<h2 id="Version_Information">Version Information</h2>
<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>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">
if (<em>condition</em>)
   <em>statement1</em>
[else
   <em>statement2</em>]
</pre>
<h2 id="Parameters" name="Parameters">Parameters</h2>
<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-US/docs/Web/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>
<h2 id="Description" name="Description">Description</h2>
<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="brush: js">
var b = new Boolean(false);
if (b) // this condition evaluates to true
</pre>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_if...else" name="Example:_Using_if...else">Example: Using <code>if...else</code></h3>
<pre class="brush: js">
if (cipher_char === from_char) {
   result = result + to_char;
   x++;
} else {
   result = result + clear_char;
}
</pre>
<h3 id="Example:_Assignment_within_the_conditional_expression" name="Example:_Assignment_within_the_conditional_expression">Example: Assignment within the conditional expression</h3>
<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="brush: js">
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="brush: js">
if ((x = y)) {
   /* do the right thing */
}
</pre>
Revert to this revision