JavaScript If Else

Last Updated Jul 29, 2015, 07:00:14 PM





JavaScript If Else Statement

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

JavaScript supports three forms of if..else statements

  • if statement
  • if...else statement
  • if...else if... statement

JavaScript if Statement

if statement the given condition matches then return the result

Syntax
Example
Try It Now

JavaScript if else statement

The if else statement will execute if the first condition fails. For example if the if condition does not pass the given condition do something else

Syntax

Example
Try It Now

Note that there is no else if syntax in JavaScript. However, you can write it with a space between else and if:

JavaScript else if... statement

Multiple else conditions in the if else condition

Syntax
Example
Try It Now

Values

condition

An expression that evaluates to true or false.

statement1

Statement that was executed if the condition evaluates to true. Can be any statement, including further nested if statements.

statement2

Statement that was executed if the condition evaluates to false, and the else clause exists. Can be any statement, including block statements and further nested if statements.



Other JavaScript conditions you might want to learn
JavaScript If Statement

JavaScript switchcase Statement

JavaScript while loop

Browser compatibility

Feature
If else Yes Yes Yes Yes Yes Yes Yes



Sources and Credits

The source of the content has been referred and updated with Mozilla Developer Network and W3C Organization

Last Updated Jul 29, 2015, 07:00:14 PM