Learn how you can control the flow of your code execution using both if and else statements. A JavaScript if…else statement evaluates whether a condition is equal to true. If a condition is equal to false, the else statement executes. Otherwise, the contents of the “if” statement run. The else statement appears after all the if conditions in a statement.

As you write your code, you will often find that you want to execute a block of your code according to some defined conditions.

For example, you may want to grant different responses for students depending on their test scores:

  • If a student got an A score, you want to write “Congratulations! You did very well.”
  • If a student got a B score, you want to write “You did great! Keep improving your skills.”

You can write a program for the above requirements by using the if statement:

let score = "A";

if (score === "A") {
  console.log("Congratulations! You did very well.");
}

if (score === "B") {
  console.log("You did great! Keep improving your skills.");
}

#javascript #programming #web-development #developer

A Guide to JavaScript if and else Statements
1.60 GEEK