Detailed description of the use of condition judgment statements in JavaScript, and detailed description of the use of javascript

Source: Internet
Author: User

Detailed description of the use of condition judgment statements in JavaScript, and detailed description of the use of javascript

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:

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:

<script type="text/javascript"><!--var age = 20;if( age > 18 ){  document.write("<b>Qualifies for driving</b>");}//--></script>

This produces the following results:

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

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:

<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:

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

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:

<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:

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.