Usage of condition judgment statements in JavaScript
This article mainly introduces the usage of condition judgment statements in JavaScript, which is the basic knowledge in JS beginners. For more information, see
When writing a program, you may need to use a path to specify two paths. Therefore, conditional statements must be used to allow the program to make correct decisions and execute correct actions.
JavaScript supports executing conditional statements based on different conditions. Here, we will explain the if... else statement.
JavaScript supports the if. else statement as follows:
If statement
If... else statement
If... else if... statement.
If statement:
An if statement is a basic control statement that allows JavaScript to make decisions and execute statements conditionally.
Syntax:
?
1 2 3 |
If (expression ){ Statement (s) to be executed if expression is true } |
Here, the JavaScript expression is evaluated. If the obtained value is true, the given statement is executed. If the expression is false, the statement is not executed. Most of the time, you will use comparative operations to make decisions.
Example:
?
1 2 3 4 5 6 7 8 |
<Script type = "text/javascript"> <! -- Var age = 20; If (age> 18 ){ Document. write ("<B> Qualifies for driving </B> "); } // --> </Script> |
This produces the following results:
?
If... else statement:
The if... else statement is the next form of the control statement, allowing JavaScript to execute more controllable statements.
Syntax
?
1 2 3 4 5 |
If (expression ){ Statement (s) to be executed if expression is true } Else { Statement (s) to be executed if expression is false } |
Here, the JavaScript expression is evaluated. If the result value is true, the given statement is executed in the if block (S. If the expression is false, the specified else statement block is executed.
Example:
?
1 2 3 4 5 6 7 8 9 10 |
<Script type = "text/javascript"> <! -- Var age = 15; If (age> 18 ){ Document. write ("<B> Qualifies for driving </B> "); } Else { Document. write ("<B> Does not qualify for driving </B> "); } // --> </Script> |
This produces the following results:
?
1 |
Does not qualify for driving |
If... else if... Syntax:
In the form of a control statement at the level of if... else if..., JavaScript makes the right decision to produce several conditions.
Syntax
?
1 2 3 4 5 6 7 8 9 |
If (expression 1 ){ Statement (s) to be executed if expression 1 is true } Else if (expression 2 ){ Statement (s) to be executed if expression 2 is true } Else if (expression 3 ){ Statement (s) to be executed if expression 3 is true } Else { Statement (s) to be executed if no expression is true } |
There is nothing special about the code. This is just a series of if Statements, where the statement before each if is part of the else clause. The statement is executed based on the true condition. If the non-condition is true, the else block is executed.
Example:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Script type = "text/javascript"> <! -- Var book = "maths "; If (book = "history "){ Document. write ("<B> History Book </B> "); } Else if (book = "maths "){ Document. write ("<B> Maths Book </B> "); } Else if (book = "economics "){ Document. write ("<B> Economics Book </B> "); } Else { Document. write ("<B> Unknown Book </B> "); } // --> </Script> |
This produces the following results:
?