Course Recommendation:

JavaScript Conditions

Conditional statements are used to perform different actions based on different conditions.

Conditional statements

When you are writing code, you often need to perform different actions based on different judgments.

You can use conditional statements in your code to achieve this.

  • to specify the new condition to be tested, if the first condition is false if In JavaScript, we can use the following conditional statements:
  • to specify the new condition to be tested, if the first condition is false else to specify the code block to be executed if the specified condition is true
  • to specify the new condition to be tested, if the first condition is false else if to specify the code block to be executed if the same condition is false
  • to specify the new condition to be tested, if the first condition is false switch to specify multiple alternative code blocks to be executed.

if statements

Please use if Statements to specify the JavaScript code block to be executed if the condition is true.

Syntax

if (Condition) {
    The code executed when the condition is true
} 

Note:if Use lowercase letters. Uppercase letters (IF or If) will cause JavaScript errors.

Example

If the time is before 18:00, then a "Good day" greeting will be issued:

if (hour < 18) {
    greeting = "Good day";
}

If the time is before 18:00, the result of greeting will be:

Good day

Try It Yourself

else statements

Please use else Statements to specify the code block to be executed if the condition is false.

if (Condition) {
    The code block executed when the condition is true
} 
    The code block executed when the condition is false
}

Example

If hour is less than 18, create a "Good day" greeting; otherwise, "Good evening":

if (hour < 18) {
    greeting = "Good day";
 }
    greeting = "Good evening";
 } 

The result of greeting:


Try It Yourself

else if statements

Please use else if to specify a new condition when the first condition is false.

Syntax

if (Condition 1) {
    The code block executed when condition 1 is true
} else if (Condition 2) {
    The code block executed when condition 1 is false and condition 2 is true
 }
    The code block executed when condition 1 and condition 2 are both false
}

Example

If the time is earlier than 10:00, create a "Good morning" greeting; if not, but the time is earlier than 18:00, create a "Good day" greeting; otherwise, create a "Good evening":

if (time < 10) {
    greeting = "Good morning";
 }
    greeting = "Good day";
 }
    greeting = "Good evening";
 } 

The result of greeting:


Try It Yourself

More Examples

Random Links
This example will write a link to CodeW3C.com or the World Wildlife Fund (WWF). Each link has a 50% chance due to the use of random numbers.

Supplementary Books

For more information on JavaScript if Statementfor more information, please read the relevant content in the Advanced JavaScript Tutorial:

ECMAScript if Statement
The if statement is one of the most commonly used statements in ECMAScript. This section will give you a detailed explanation of how to use the if statement.