JavaScript If... Else Declaration

Source: Internet
Author: User

The condition declaration in JavaScript is used to complete the action under different conditions.
Condition Statement
When writing code, you often need to perform different behaviors according to different conditions. You can use the condition declaration in the code to complete this task.
In JavaScript, we can use the following conditional declarations:
If statement
Run the code when a specified condition is set.
If... else Declaration
Execute the code when the specified condition is set. If the condition is not set, execute another code.
If... else Declaration
You can use this statement to execute one of several block codes.
Switch Declaration
You can use this statement to execute one of several block codes. If statement
If you want to execute code when the specified condition is set, you can use this statement.
Syntax:
Copy codeThe Code is as follows:
If (condition)
{
Run the code when the condition is set.
}

Note: Use lowercase letters. An error occurs when uppercase IF is used!
Instance 1

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Instance 2

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Note: Use the equal sign (=) to compare variables!
Note: There is no else in the syntax. The code is executed only when the condition is true.
If... else Declaration
If you want to execute a piece of code when the condition is set, and execute another piece of code when the condition is not set, you can use the if... else declaration.
Syntax:
If (condition)
{
Run this code when the condition is set.
}
Else
{
Run this code when the condition is invalid.
}
Instance

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
If... else if... else Declaration
When you need to select one of multiple sets of code to run the program, use if... else declaration.
Syntax:
Copy codeThe Code is as follows:
If (condition 1)
{
Run the code when condition 1 is set.
}
Else if (condition 2)
{
Run the code when condition 2 is set.
}
Else
{
Run the code when neither condition 1 nor condition 2 is successful.
}

Instance:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.