Home  >  Article  >  Web Front-end  >  [JavaScript Tutorial] JavaScript If...Else Statement

[JavaScript Tutorial] JavaScript If...Else Statement

黄舟
黄舟Original
2016-12-24 15:15:001083browse

JavaScript If...Else Statement

Conditional statements are used to perform different actions based on different conditions.

Conditional Statements

Usually when writing code, you always need to perform different actions for different decisions. You can use conditional statements in your code to accomplish this task.

In JavaScript, we can use the following conditional statements:

if statement - Use this statement to execute code only when the specified condition is true

if...else statement - Execute code when the condition is true, when Execute other code when the condition is false

if...else if....else statement - Use this statement to select one of multiple code blocks to execute

switch statement - Use this statement to select one of multiple code blocks First, execute the

If statement

This statement will execute the code only when the specified condition is true.

Syntax

if (condition)
  {
 当条件为 true 时执行的代码
  }

Please use lowercase if. Using uppercase letters (IF) will generate a JavaScript error!

Example

When the time is less than 20:00, generate the greeting "Good day":

if (time<20)
  {
  x="Good day";
  }
<p

x The result is:

Good day

Please note that in this syntax, there is no ..else... You've told the browser to only execute the code if the specified condition is true.

If...else statement

Please use if....else statement to execute code when the condition is true and other code when the condition is false.

Syntax

if (condition)
  {
  当条件为 true 时执行的代码
  }
else
  {
  当条件不为 true 时执行的代码
  }

Example

When the time is less than 20:00, generate the greeting "Good day", otherwise generate the greeting "Good evening". The result of

if (time<20)
  {
  x="Good day";
  }
else
  {
  x="Good evening";
  }

x is:

Good day

If...else if...else statement

Use the if....else if...else statement to select one of multiple blocks of code to execute.

Syntax

if (condition1)
  {
  当条件 1 为 true 时执行的代码
  }
else if (condition2)
  {
 当条件 2 为 true 时执行的代码
  }
else
  {
  当条件 1 和 条件 2 都不为 true 时执行的代码
  }

Example

If the time is less than 10:00, generate the greeting "Good morning", if the time is greater than 10:00 and less than 20:00, generate the greeting "Good day", otherwise generate the greeting "Good evening" :

if (time<10)
  {
  x="Good morning";
  }
else if (time>=10 && time<20)
  {
  x="Good day";
  }
else
  {
  x="Good evening";
  }

x The result is:

Good day

The above is the content of the [JavaScript Tutorial] JavaScript If...Else statement. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn