Tuesday 27 February 2018

Learn to understand the use of operators in JavaScript

There are different types of operators in JavaScript that you can use to perform mathematical operations or design values to variables. Let’s create a webpage named OperatorPrecedence.html to understand the use of operators.

####Showing the code of for the OperatorPrecedence.html File.#####

<!DOCTYPE html>
<html>
                <head>
                                <title>Using operators</title>
                </head>
                <body>
                                <h1>Using operators in the script</h1>
                                <SCRIPT type=”text/javascript”>
                                                var math=95, English=80, Science=90, avgMarks;
                                                avgMarks= math + English+ Science/3;
                                                document.write(“Average Marks = ” + avgMarks);
                                                avgMarks=(math + English + Science) / 3;
                                                document.write(“<BR/>Average Marks = “ + avgMarks);
                                </SCRIPT>
                </body>
</html>

The script within the starting and the ending tags of body has four variables named math,Englsih, Science and avgMarks. These variables store the marks of all the four subjects as well as the average marks. The avgMarks variable contains the value expression math + science+ English/3. Note that in this case English/3 is evaluated at first whereas, in the next statement we have given the variable be (math + English + Science) / 3. In this case fist sum of marks is divided by 3 which gives the exact average.

#####Showing the output of the OperatorPrecedane.html file####




In the above image you can observe the result of the first expression is 205 whereas that of the second expression is 88.333333333333.Now let’s discuss the IF statement.




No comments:

Post a Comment