Page 338 - Webapplication11_C11_Flipbook
P. 338

For example:

                // alert("Hello"); Single line comment
                /* a = 10;
                b = 20; Multiline comment
                */

              Operators
              Operators are special symbols that are used to perform mathematical, logical, and relational operations on values and
              variables. The variables or values on which the operators work are called operands. Some commonly used operators in
              JavaScript are arithmetic operators,  assignment operators or shorthand assignment operators, comparison operators,
              logical operators, etc.
              Arithmetic Operators

              Arithmetic operators are used to perform mathematical operations on numbers in JavaScript. JavaScript provides the
              following arithmetic operators:

                Operator     Name                           Description                         Example       Output
                                                                                              (a = 11, b = 4)
                   +      Addition      Adds two operands.                                        a + b         15
                   –      Subtraction   Subtracts  the  operand  written  on  the  right-hand  side  of  the   a – b  7
                                        operator from the operand written on the left side.
                   *      Multiplication  Multiplies both operands.                               a * b         44

                   /      Division      Divides numerator by denominator.                         a / b        2.75

                   %      Modulus       Divides numerator by denominator and returns the remainder.  a % b      3

                   **     Exponent      Calculates  the  base  to  the  exponent  power,  that  is  base^   b**2  16
                                        exponent

                   ++     Increment     Used to increase the value of a variable by 1.            ++a           12
                   --     Decrement     Used to decrease the value of a variable by 1.            --a           10

              Example: 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>
                336     Touchpad Web Applications-XI
   333   334   335   336   337   338   339   340   341   342   343