JavaScript Conditional Statements - PART - 13
JavaScript Conditional Statements - PART - 13
IF (Example)
1. If statement
2. If…Else statement
3. If…Else If…Else statement
If statement
Syntax:
if (condition)
You can use If statement if you want to check only a specific condition.
<html>
<head>
<title>IF Statments!!!</title>
<script type="text/javascript">
var age = prompt("Please enter your age");
if(age>=18)
document.write("You are an adult <br />");
if(age<18)
document.write("You are NOT an adult <br />");
</script>
</head>
<body>
</body>
</html>
Output:
If…Else statement
Syntax:
if (condition)
else
You can use If….Else statement if you have to check two conditions and execute a
different set of codes.
<html>
<head>
<title>If...Else Statments!!!</title>
<script type="text/javascript">
// Get the current hours
var hours = new Date().getHours();
if(hours<12)
document.write("Good Morning!!!<br />");
else
document.write("Good Afternoon!!!<br />");
</script>
</head>
<body>
</body>
</html>
Output:
if (condition1)
{
lines of code to be executed if condition1 is true
else if(condition2)
else
You can use If….Else If….Else statement if you want to check more than two conditions.
Output: