The conditional judgment statement in JavaScript uses the detailed knowledge _ basics

Source: Internet
Author: User

In writing a program, there may be a situation when you need to take a path out of a given two paths. Therefore, you need to use conditional statements to let programs make the right decisions and perform the right actions.

JavaScript supports its use to perform operating condition statements that differ according to different conditions. Here, we will explain if. Else statement.

JavaScript supports IF.. The Else statement forms the following:

    • 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 conditionally execute statements.
Grammar:

if (expression) {
  Statement (s) to was executed if expression is true
}

The JavaScript expression here expression evaluated. If the resulting value is true, the given statement executes. If the expression is false, the declaration is not executed. Most of the time you will use comparison operations when using decisions.
Example:

<script type= "Text/javascript" >
<!--
var age =;
if (age >) {
  document.write (' <b>qualifies for driving</b> ');
}
-->
</script>

This will produce 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.
Grammar

if (expression) {
  Statement (s) to be executed if expression am true
}else{
  Statement (s) to be executed if Expre Ssion is False
}

Here 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 set ELSE statement block is executed.
Example:

<script type= "Text/javascript" >
<!--
var age =;
if (age >) {
  document.write (' <b>qualifies for driving</b> ');
} else{
  document.write ("<b>does not qualify for driving</b>");
}
-->
</script>

This will produce the following results:

Does not qualify for driving


If...else If ... Syntax:

In the If...else if .... The control statement of a level-pushing form of JavaScript makes the right decisions out of several conditions.
Grammar

if (expression 1) {
  Statement (s) to am executed if expression 1 is true
}else if (expression 2) {
  Statement (s) t o be executed if expression 2 am true
}else if (expression 3) {
  Statement (s) to being executed if expression 3 is true
}else{
  Statement (s) to was executed if no expression is true
}

There is nothing special about the code. This is just a series of if statements, where statements preceding each if are part of the ELSE clause. The declaration is executed based on a true condition, and if the condition is true, the else block executes.
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 will produce the following results:

Maths Book

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.