Posts

Showing posts from October, 2020

REST PARAMETERS AND THE SPREAD OPERATOR

It can be very useful for a function to accept any number of arguments. For example, Math.max computes the maximum of all arguments it is given But how can we write such a function  well the answer is rest parameter look at this code: function   max (... numbers ) {      let   result  = - Infinity ;      for  ( let   number   of   numbers ) {           if  ( number  >  result )  result  =  number ;     }      return   result ; } console . log ( max ( 4 ,  1 ,  9 , - 2 )); // → 9 in the above example the number argument contains all the value as an array (i,e. [4,1,9,-2]) , and it is our rest parameter . a rest parameter takes extra arguments and hold them in an array.And the triple dot(...) sign is called our spread operator . for example: function   restP...