Tuesday 27 February 2018

How to use IF Statement in JavaScript

We can use the IF statement to execute a group of one or more script statements only, when a particular condition is met. Let’s create a webpage, named IfStatement.html which shows the use of if statement.

#####Showing the code of the IfStatement.html file#####

<!DOCTYPE html>
<html>
                <head>
                                <title>Using if statement</title>
                </head>
                <body>
                                <h1>Using the if statement in the script</h1>
                                <SCRIPT type=”text/javascript”>
                                                var Number=45;
                                                if((Number%2)) !=0)
                                                {
                document.write(Number +" is an odd number");
}
                                                document.write("<BR/> Thank you");
</SCRIPT>
                </body>
</html>
The script enclosed within the starting and the ending tags of the body element has a variable named Number that is assigned a value 45. The if statement has a condition which checks if the number is divisible by 2. If the number is not divisible by 2 the statement within curly brackets are executed and if the number is divisible by 2 the statement within the curly bracket is ignored and the immediate statement is executed. 

#####Showing the Output of the IfStatement.html file######




No comments:

Post a Comment