2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: If-Else Statement
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Introduction
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `if`  statement executes a statement if a specified condition is `true` . If the condition is `false` , another statement can be executed using the `else`  statement.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Note:** The `else`  statement is optional.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								if (condition) {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    /* do something */
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								} else {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    /* do something else */
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Multiple `if...else`  statements can be chained to create an `else if`  clause. This specifies a new condition to test and can be repeated to test multiple conditions, checking until a true statement is presented to execute.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								if (condition1) {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    /* do something */
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								} else if (condition2) {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    /* do something else */
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								} else if (condition3) {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    /* do something else */
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								} else {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    /* final statement */
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Note:** If you want to execute more than one statement in the `if` , `else`  or `else if`  part, curly braces are required around the statements:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if (condition) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /* do */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /* something */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /* with multiple statements */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /* do something */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    /* else */
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< a  href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else'  target = '_blank'  rel = 'nofollow' > MDN link< / a >  | < a  href = 'https://msdn.microsoft.com/en-us/library/85yyde5c.aspx'  target = '_blank'  rel = 'nofollow' > MSDN link< / a >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-20 18:47:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Additional ternary operator
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For a simple operation like assigning a value to a variable conditionally you can use a ternary operator, which is a shorthand syntax for an `if...else`  clause in JavaScript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Normal if...else
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								let num  = 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if (someCondition){
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    num = 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    num = 0;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Using ternary operator
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const num = someCondition ? 1 : 2;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// condition ? true case : false case
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								## Examples
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Using** `if...else` :
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// If x = 5, then z = 7 and q = 42.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// If x is not 5, then z = 19.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								if (x == 5) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  z = 7;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  q = 42
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  z = 19;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Using** `else if` :
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
									
										
										
										
											2018-12-01 12:17:12 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								if (x <  10 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return "Small number";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} else if (x <  50 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return "Medium number";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} else if (x <  100 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return "Large number";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								} else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  flag = 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return "Invalid number";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-11-22 00:26:07 +11:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Using** `if`  **alone** :
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```javascript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // This function can also act as a Boolean
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (x <  30 )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return "true";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }