Default Parameters
The default parameter came to handle function parameters with ease. You can easily set Default parameters to allow initializing formal parameters with default values. This is possible only if no value or undefined is passed.
Example
<html> <body> <script> // default is set to 1 function inc(val1, inc = 1) { return val1 + inc; } document.write(inc(10,10)); document.write("<br>"); document.write(inc(10)); </script> </body> </html>
Output
Rest Parameters
ES6 brought rest parameter to ease the work of developers. For arguments objects, rest parameters are indicated by three dots … and precedes a parameter. With this, set indefinite number of arguments as an array, which is Array instances.
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>