Tuesday 27 February 2018

Learn How to use if-else Statement in JavaScript

The if-else statement executes the code written in the if statement if the specified condition is true. Otherwise the code written in the else statement is executed. Now create a Web page named IfELSEStatement.html which show the use of if..else statement.

#####Showing the code for IFELSEStatement.html File #######
<!DOCTYPE html>
<head>
<title>IF--ELSE Statement</title>
</head>
</body>
<h1>Using the if...else Statement in the script</h1>
<SCRIPT type="text/javascript">
var Number=44;
if((Number%2)!=0)
{
document.write(Number+"is an odd number");
}
else
{
document.write(Number+"is an even number");
}
document.write("<br/>Thank You Very Much!");
</SCRIPT>
</body>
</html>

The Number variable contains the value 44. The condition in the if...else statement checks whether or not the Number variable is divisible by 2. If the number variable is divisible by 2, then the execution passes on to the else block and the statement within the curly braces after the else block is executed.




Here Above We can observe that the if..else statement checks the expression to find out whether the value 44 is even or odd.

No comments:

Post a Comment