Javascript: If Else

The If…Else… statement in Javascript is similar to other programming languages such as C.

It will first check if a condition is true and will execute a block of code if the condition is met.

<!DOCTYPE html>
 <title>If Else</title>
 function myFunction() {
   var passing_score = 60;
   var your_score = 70;
   if (your_score > passing_score) {
      document.getElementById("str1").innerHTML = "You have passed";
      } else {
      document.getElementById("str1").innerHTML = "You failed";
  <p id="str1">Passed or failed</p>
  <button onclick="myFunction()">Click Me</button>


As your_score, which is 70 is greater than passing_score which is 60, the first condition is output in the browser.

In the fiddle, you can try changing the value for passing_score or your_score to see what is output in the browser.

Series Navigation<< Javascript: Operators
Javascript: While loop >>