JavaScript – Decision Making Statements

With decision-making statements in JavaScript, easily take decisions based on different conditions. The following are the decision-making statements in JavaScript:

  • if statement
  • if…else statement
  • if…elseif..else statement

if statement

The if statement in JavaScript executes the code if the condition is true. The following is the syntax of the if statement:

if (condition) {
   // statement(s) gets executed if the condition is True
}

Let us see an example to implement the if statement in JavaScript:

<!DOCTYPE html>
<html>
<body>

<h1>if statement</h1>
<h2>Vote Status</h2>

<p id="test"></p>

<script>
let age = 20 

// Check the vote eligibility
if (age > 18) {
  document.getElementById("test").innerHTML = "The Candidate is Eligible to Vote";
}
</script>

</body>
</html>

Output

JavaScript if statement

if…else statement

The if…else statement in JavaScript executes some code if the condition is true and another code if
the condition is false. The false condition comes under else. The following is the syntax of the if…else statement:

if (condition) {
   // code will execute if the condition is true
}
else {
   // code will execute if the condition is false
}

Let us see an example to implement the if…else statement in JavaScript:

<!DOCTYPE html>
<html>
<body>

<h1>if...else statement</h1>
<h2>Vote Status</h2>

<p id="test"></p>

<script>
let age = 15 

// Check the vote eligibility
if (age > 18) {
 result = "The Candidate is Eligible to Vote"
}
else {
 result = "The Candidate is not Eligible to Vote"
}

 document.getElementById("test").innerHTML = result;
</script>
</body>
</html>

Output

JavaScript if...else statement

if…else if…else statement

The if…else if…else statement in JavaScript executes code for different conditions. If more than 2 conditions are true, you can use else for the false condition.

The following is the syntax of the if…else if…else statement:

if (condition1) {
   // code will execute if the condition is true
} else if (condition2) {
   // code will execute if condition2 is true
} else if (condition3) {
   // code will execute if condition3 true
} else {
   // code will execute if all the above given conditions are false
}

Let us see an example to implement the if…else if…else statement in JavaScript:

<!DOCTYPE html>
<html>
<body>

<h1>if...else if...else statement</h1>
<h2>Vote Status</h2>

<p id="test"></p>

<script>
let age = 15 

// Check the vote eligibility
if (age > 18) {
 result = "The Candidate is Eligible to Vote"
}
else if (age == 18){
 result = "Age is 18, therefore the candidate can vote."
}
else {
  result = "Age is less than 18, therefore the candidate cannot vote."
}

 document.getElementById("test").innerHTML = result;
</script>

</body>
</html>

Output

JavaScript if...elseif...else statement

JavaScript Tutorial
JavaScript Loops
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment