Usage of condition judgment statements in JavaScript

Source: Internet
Author: User

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:

?

1

Qualifies for driving

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:

?

 

1

Maths Book

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.