Course recommendation:

JavaScript conditions

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

Conditional statements

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

You can implement this in your code using conditional statements.

  • 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 that can be executed

if statement

Use if Statement to specify the JavaScript code block to be executed when 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.

Voorbeeld

If the time is before 18:00, then the greeting will be 'Good day':

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

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

Good day

Probeer het zelf

else statement

Use else Statement to specify the code block to be executed when the condition is false

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

Voorbeeld

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

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

Het resultaat van greeting:


Probeer het zelf

else if statement

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) {
    When condition 1 is false and condition 2 is true, the code block is executed
 } else {
    When both condition 1 and condition 2 are false, the code block is executed
}

Voorbeeld

Als de tijd vroeger is dan 10:00, wordt er een "Good morning"-groet gemaakt, als dat niet het geval is, maar de tijd vroeger is dan 18:00, wordt er een "Good day"-groet gemaakt, anders wordt een "Good evening"-groet gemaakt:

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

Het resultaat van greeting:


Probeer het zelf

Meer voorbeelden

Willekeurige link
Deze voorbeeld zal een link naar CodeW3C.com of de World Wide Fund for Nature (WWF) schrijven. Door het gebruik van willekeurige getallen heeft elke link een 50% kans.

Buitenboek

Meer over JavaScript if-statementMeer informatie, lees de relevante inhoud in de JavaScript Avanceerd Handleiding:

ECMAScript if-statement
De if-statement is een van de meest gebruikte statements in ECMAScript. Deze sectie biedt een gedetailleerde uitleg van hoe je de if-statement kunt gebruiken.