Course recommendation:
- Poprzednia strona Porównanie JS
- Następna strona JS Switch
JavaScript condition
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 by using conditional statements in your code.
- 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.
实例
If the time is before 18:00, send the greeting "Good day":
if (hour < 18) { powitanie = "Good day"; }
If the time is before 18:00, the result of greeting will be:
Good day
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 } The code block is executed when the condition is false }
实例
If hour is less than 18, create a "Good day" greeting, otherwise "Good evening":
if (hour < 18) { powitanie = "Good day"; } powitanie = "Good evening"; }
Wynik powitania:
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 } When both condition 1 and condition 2 are false, the code block is executed }
实例
Przykład
Jeśli czas jest wcześniejszy niż 10:00, utwórz powitanie "Good morning", jeśli nie, ale czas wcześniejszy niż 18:00, utwórz powitanie "Good day", w przeciwnym razie utwórz powitanie "Good evening": powitanie = "Good morning"; } powitanie = "Good day"; } powitanie = "Good evening"; }
Wynik powitania:
Więcej przykładów
- Losowy link
- Ten przykład doda link do CodeW3C.com lub Światowej Organizacji Ochrony Przyrody (WWF). Dzięki użyciu liczb losowych, każdy link ma 50% szansy.
książki pozaszkolne
Chcesz dowiedzieć się więcej o Zdanie if w JavaScriptWięcej informacji, proszę przeczytać związane treści w zaawansowanym tutorialu JavaScript:
- Zdanie if w ECMAScript
- Zdanie if jest jednym z najczęściej używanych zdań w ECMAScript. W tym rozdziale szczegółowo wyjaśniamy, jak używać zdań if.
- Poprzednia strona Porównanie JS
- Następna strona JS Switch