DEV Community

loading...

Setting Default Parameters for Your Functions

rthefounding profile image Randy Rivera ・1 min read

In order to help us create more flexible functions, ES6 introduces default parameters for functions.

  • Ex:
const increment = (number, value = 1) =>  number + value;
Enter fullscreen mode Exit fullscreen mode
console.log(increment(5)); will display 6
console.log(increment(5, 2)); will display 7
Enter fullscreen mode Exit fullscreen mode

As you can see in the example above, we added a default parameter so that it will add 1 to number if value is not specified.

Discussion (0)

Forem Open with the Forem app