Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript if... Else statement


May 06, 2021 JavaScript


Table of contents


JavaScript if... Else statement


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


Conditional statement

Often when you write code, you always need to perform different actions for different decisions. You can use conditional statements in your code to accomplish the task.

In JavaScript, we can use the following conditional statements:

  • if statement - Use the statement to execute code only if the specified condition is true
  • if... Else statement - code is executed when the condition is true and other code is executed when the condition is false
  • JavaScript tri-eye operation - code is executed when the condition is true and other code is executed when the condition is false
  • if... e lse if.... else statement - Use that statement to select one of several blocks of code to execute
  • Switch statement - Use this statement to select one of several blocks of code to execute

If statement

The statement executes the code only if the specified condition is true.

Grammar

if ( condition )
{
Code performed when the condition is TRUE
}

Use a small-ced if. Using capital letters (IF) generates JavaScript errors!

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

if (time<20)
{
x="Good day";
}
The result of x is:
Good day

Try it out . . .

Note that in this syntax, there is no .. e lse..。 You have told the browser to execute the code only if the specified condition is true.


If... Else statement

Please use if.... The else statement executes code when the condition is true and other code when the condition is false.

Grammar

if ( condition )
{
Code performed when the condition is TRUE
}
else
{
Code that is not performed when the condition is not TRUE
}

When the time is less than 20:00, the greeting "Good day" is generated, otherwise the greeting "Good evening" is generated.

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

The result of x is:

Good day

Try it out . . .

Tip: In this site's programming battle, you can practice how to use JavaScript's if statement!



Javascript tri-operation (triple operation) statement

Please use ( condition1 ) ? t ure-doing : else-doing; The statement executes code when the condition is true and other code when the condition is false.
Instance
5 > 3 ? alert("5大于3") : alert("5小于3");
Note: if... The difference between else and tri-eye operation is summed up in one sentence: tri-eye operation has a return value, if else does not return a value

Example 1:

var n=1;
if(n>1){
    n=0;
}else{
    n++;
}
console.log(n);
#输出结果:2

var n=1;
n = n>1?0 : n++;
console.log(n);
#输出结果为:1

Example 2:

var n=1;
if(n>1){
    n=0;
}else{
    ++n;
}
console.log(n);
#输出结果:2

var n=1;
n = n>1?0 : ++n; 
console.log(n); 
#输出结果为:2


If... e lse if... Else statement

Use if.... e lse if... The else statement selects one of several blocks of code to execute.

Grammar

if ( condition1 )
{
Code performed when the condition 1 is TRUE
}
else if ( condition2 )
{
Code performed when condition 2 is TRUE
}
else
{
Code that is not performed when condition 1 and condition 2 are true
}

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

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

The result of x is:

Good morning

Try it out . . .

Tip: When using if and else if statements, you need to pay attention to the order in which the code is executed, please refer to this site's JavaScript Programming Practice!


JavaScript if... Else statement

More instances

Random links
This example demonstrates a link that takes you to a different place when you click on it. Each opportunity has a 50% probability.

Read about it

JavaScript Standard Reference Manual: Javascript if conditions