Powered by Blogger.

Thursday, February 27, 2014

If Else statements in JavaScript Example



In this post we will discuss how to use if else statement in javascript. Also you can check my previous posts  on:

- Transaction in SQL Server 2008

- Display Favicon in Asp.Net web site

- How To Call a Button Click From Another Button in C#.Net?

Syntax of if else statement:

if (condition) {
    statements1;
}
else if (condition)  {
    statements2;
}
else if (condition)  {
    statements3;
}
else{
    statements4;
}

Example:

Below is an example of if else statement:

<script language="JavaScript" type="text/javascript">

        var age = eval(prompt("How much you scored?", ""));

        if (age >= 60) {
            alert("First Division");
        }
        else if (age > 50 && age < 60) {
            alert("Second Division");
        }
        else if (age >= 30 && age < 50) {
            alert("Third Division");
        }
        else {
            alert("Fail");
        }

    </script>

Here in this example it will first prompt use to enter a value and according to the condition it will alert the message. See fig below:


Once you enter the value it will show alert box according to the condition like below:



0 comments

Post a Comment