DEV Community

Discussion on: Default Parameters in ES6

Collapse
 
karfau profile image
Christian Bewernitz

This also works:

function destructedDefaults({foo="default"}={}) {
  return foo;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sarah_chima profile image
Sarah Chima

Nice, but what's the purpose of the second curly brackets?

Collapse
 
karfau profile image
Christian Bewernitz

Well, it is the default for the first argument.
So you can call the function with no argument.
If it wouldn't be there, calling it without an argument, destruction leads to a TypeError of can not access property 'foo' of undefined.

Thread Thread
 
sarah_chima profile image
Sarah Chima

Okay cool