What are Rest parameters in JavaScript functions?

JavascriptWeb DevelopmentFront End Technology

ES6 brought rest parameter to ease the work of developers. For arguments objects, rest parameters are indicated by three dots … and precede a parameter.

Example

Let’s see the following code snippet −

<html>
   <body>
      <script>
         function addition(…numbers) {
            var res = 0;
            numbers.forEach(function (number) {
               res += number;
            });
            return res;
         }
         document.write(addition(3));
         document.write(addition(5,6,7,8,9));
      </script>
   </body>
</html>
raja
Published on 16-Feb-2018 12:27:56
Advertisements