DEV Community

Discussion on: Default Function Parameters (ES6)

Collapse
 
joelnet profile image
JavaScript Joel • Edited

Default values are one of the best additions to JavaScript.

I'd like to mention in this example, using the || short circuit evaluation will result in bugs.

function getSum(a, b){
  a = a || 1;
  b = b || 41;
  return a + b;
}

getSum(0, 0) //=> 42

In this example, getSum(0, 0) will return 42.

The reason is 0 is falsy and therefore will result in 1 for a and 41 for b.

Cheers!

Collapse
 
runosaduwa profile image
Runo-saduwa

Yes in fact, Thank you for spreading more light on that path. That's why the default function parameters is indeed a boon to JavaScript. We're saved from these quirks that could leave us banging our heads on our monitors all day long!... Thanks for reading and happy coding!!