Page 67 - JavaScript
P. 67

}


        Now we can call delta() function passing any integer, both positive and negative, as delta
        parameter.




        Using modulus to obtain the fractional part of a number



          var myNum = 10 / 4;       // 2.5
          var fraction = myNum % 1; // 0.5
          myNum = -20 / 7;          // -2.857142857142857
          fraction = myNum % 1;     // -0.857142857142857


        Incrementing (++)


        The Increment operator (++) increments its operand by one.


            •  If used as a postfix, then it returns the value before incrementing.
            •  If used as a prefix, then it returns the value after incrementing.



         //postfix
         var a = 5,    // 5
             b = a++,  // 5
             c = a     // 6


        In this case, a is incremented after setting b. So, b will be 5, and c will be 6.



         //prefix
         var a = 5,    // 5
             b = ++a,  // 6
             c = a     // 6


        In this case, a is incremented before setting b. So, b will be 6, and c will be 6.




        The increment and decrement operators are commonly used in for loops, for example:


         for(var i = 0; i < 42; ++i)
         {
           // do something awesome!
         }


        Notice how the prefix variant is used. This ensures that a temporarily variable isn't needlessly
        created (to return the value prior to the operation).


        Decrementing (--)


        The decrement operator (--) decrements numbers by one.




        https://riptutorial.com/                                                                               24
   62   63   64   65   66   67   68   69   70   71   72