Course recommendation:
- Προηγούμενη σελίδα JS Σύγκριση
- Επόμενη σελίδα JS Σwith
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 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.
实例
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
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) { greeting = "Good day"; } greeting = "Good evening"; }
Το αποτέλεσμα του greeting:
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 }
实例
Παράδειγμα
Αν η ώρα είναι νωρίτερα από 10:00, δημιουργήστε την ευχή "Good morning", αν όχι, αλλά η ώρα είναι νωρίτερα από 18:00, δημιουργήστε την ευχή "Good day", αλλιώς δημιουργήστε την ευχή "Good evening": greeting = "Good morning"; } greeting = "Good day"; } greeting = "Good evening"; }
Το αποτέλεσμα του greeting:
Περισσότερα παραδείγματα
- Τυχαίοι σύνδεσμοι
- Αυτό το παράδειγμα θα γράψει σύνδεσμοι στο CodeW3C.com ή στη Διεθνή Ένωση για τη Βiodiversität (WWF). Χρησιμοποιώντας τυχαία αριθμούς, κάθε σύνδεσμος έχει 50% πιθανότητα.
Εκπαιδευτικά βιβλία
Για περισσότερες πληροφορίες σχετικά με Πρόταση JavaScript ifΓια περισσότερες πληροφορίες, διαβάστε τα σχετικά περιεχόμενα στο πρόγραμμα σπουδών JavaScript προχωρημένων:
- Γραφήματα ECMAScript if
- Η πρόταση if είναι μια από τις πιο συχνά χρησιμοποιούμενες προτάσεις στην ECMAScript. Αυτό το κεφάλαιο σας εξηγεί λεπτομερώς πώς να χρησιμοποιήσετε την πρόταση if.
- Προηγούμενη σελίδα JS Σύγκριση
- Επόμενη σελίδα JS Σwith