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 to be executed

if statement

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

Syntax

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

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

Beispiel

If the time is before 18:00, then send the greeting "Good day":

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

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

Good day

Probieren Sie es selbst aus

else statement

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

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

Beispiel

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

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

Das Ergebnis von greeting:


Probieren Sie es selbst aus

else if statement

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

Syntax

if (Condition 1) {
    The code block is 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
}

Beispiel

Wenn die Zeit vor 10:00 Uhr liegt, wird eine "Good morning"-Grüße erstellt. Wenn nicht, aber die Zeit vor 18:00 Uhr liegt, wird eine "Good day"-Grüße erstellt. Andernfalls wird eine "Good evening"-Grüße erstellt:

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

Das Ergebnis von greeting:


Probieren Sie es selbst aus

Mehr Beispiele

Zufällige Links
Dieser Beispielcode schreibt einen Link zu CodeW3C.com oder zur Welttierschutzorganisation (WWF). Durch die Verwendung zufälliger Zahlen hat jeder Link eine 50%-ige Chance.

Fachliteratur

mehr über JavaScript if-Anweisungweitere Informationen, lesen Sie bitte die entsprechenden Inhalte im JavaScript-Aufbauanleitung:

ECMAScript if-Anweisung
Die if-Anweisung ist eine der häufigsten Anweisungen in ECMAScript. In diesem Abschnitt wird detailliert erläutert, wie die if-Anweisung verwendet wird.