How to define rest parameter for a function in JavaScript?

JavascriptWeb DevelopmentFront End Technology

With rest parameter, you can represent a number of arguments as an array. ES6 brought rest parameter to ease the work of developers. For arguments objects, rest parameters are indicated by three dots … and precedes a parameter.

Example

Let’s see the following code snippet to define rest parameter.

<html>
   <body>
      <script>
         function addition(…numbers) {
            var res = 0;
            numbers.forEach(function (number) {
               res += number;
            });
            return res;
         }
         document.write(addition(3));
         document.write(addition(9,10,11,12,13));
      </script>
   </body>
</html>
raja
Published on 25-Jan-2018 11:24:22
Advertisements