Tuesday, May 14, 2024

JavaScript if-else statement

JavaScript If-else
The JavaScript if-else statement is used to execute the code whether condition is true or false. There are three forms of if statement in JavaScript.

1)    If Statement
2)    If else statement
3)    if else if statement

JavaScript If statement

It evaluates the content only if expression is true. The signature of JavaScript if statement is given below.

    
if(expression){  
   //content to be evaluated  
}

<!DOCTYPE>
<html>
<head>
    <title>JavaScript</title>
    <script>
         /* Greater Than If Condition */
        var a = 100;
        var b = 20;

        if(a > b) {
            document.write("A is Greater");
        }
        document.write("<br><br>");

        /* Equal to If Condition */
        var m = 100;
        var n = 100;

        if (m == n) {
            document.write("A is Greater");
        }
        document.write("<br><br>");

        /* Equal value and equal type If Condition */
        var x = 100;
        var y = "100";

        if (x === y) {
            document.write("Yahoo Baba");
        }
        document.write("<br><br>");    
    </script>
</head>
<body>
</body>
</html>

JavaScript If...else Statement

It evaluates the content whether condition is true or false. The syntax of JavaScript if-else statement is given below.

if(expression){  
   //content to be evaluated if condition is true  
}  
else{  
   //content to be evaluated if condition is false  
}


<!DOCTYPE>
<html>
<head>
    <title>JavaScript</title>
    <script>
        /* Greater Than If Else Condition */
        var a = 15;
      
        if (a > 30) {
            document.write("A is Greater");
        }else {
            document.write("A is smaller");
        }
        document.write("<br><br>");
        
        /* Equal to If Else Condition */
        var x = 100;
        
        if (x == 100) {
            document.write("X is Same");
        }else {
            document.write("X is not smaller");
        }
    </script>
</head>
<body>
</body>
</html>


JavaScript If...else if statement

It evaluates the content only if expression is true from several expressions. The signature of JavaScript if else if statement is given below.

if(expression1){  
     //content to be evaluated if expression1 is true  
}  
else if(expression2){  
    //content to be evaluated if expression2 is true  
}  
else if(expression3){  
    //content to be evaluated if expression3 is true  
}  
else{  
    //content to be evaluated if no expression is true  
}  

<!DOCTYPE>
<html>
<head>
    <title>JavaScript</title>
    <script>
        /* If Else IF Condition */
        var per = prompt("Enter your Percentage : ");

        if (per >= 80 && per <= 100) {
            document.write("You are in Merit.");
        }else  if (per >= 60 && per < 80) {
            document.write("You are in Ist Division.");
        }else if (per >= 45 && per < 60) {
            document.write("You are in IIst Division.");
        }else if (per >= 33 && per < 45) {
            document.write("You are in IIIst Division.");
        }else if (per < 33) {
            document.write("You are Fail.");
        }else {
            document.write("Please Enter Valid Percentage.");  
        }
    </script>
</head>
<body>
</body>
</html>

No comments:

Post a Comment