Page 193 - Web_Application_v2.0_C12_Fb
P. 193

Example 4: To calculate the square and cube of two numbers using JavaScript arithmetic operators

                   <HTML>
                   <BODY>

                   <SCRIPT>
                   var num=5;
                   var sq=num**2;

                   var cube=num**3;
                   document.writeln("Square of "+num+"="+sq);

                   document.write("<br> <br>");
                   document.write("Cube of "+num+"="+cube);

                   </SCRIPT>
                   </BODY>

                   </HTML>
                 Output:















                            Notes


                           The difference between ‘/’ and ‘%’ operators is that ‘/’ operator gives the quotient and ‘%’ operator gives
                           the remainder obtained during division.



                 Assignment Operators
                 Assignment operators are used to assign the value of the right-hand operand to the left-hand operand.
                 JavaScript has only one assignment operator (=). Some other assignment operators are created by combining
                 the assignment operator with an arithmetic operator. These operators are also called shorthand assignment
                 operators or compound assignment operators.

                 JavaScript provides the following assignment operators:

                  Operator      Name                           Description                        Example       Output
                                                                                                   (a=3)
                     =      Assignment    It assigns the value of the right operand to the left operand.  a = 3   3

                     +=     Addition      It adds right operand to the left operand and assigns the   a += 3      6
                            assignment    result to left operand. x+=3 is equivalent to x=x+3.
                     –=     Subtraction   It subtracts right operand from the left operand and assigns   a – = 3  0
                            assignment    the result to left operand. x–=3 is equivalent to x=x–3.



                                                                                                  JavaScript Part 2  191
   188   189   190   191   192   193   194   195   196   197   198