Default Function Parameters in JavaScript

As of ES6, we are able to set default parameter values when defining a function, as shown below:

function calculateBill(total, tax = 0.13, tip = 0.15) {...}

This allows for named parameters to be initialized with default values if no value is present or it evaluates to undefined.

If you want to call a function without passing one of the parameters, allowing it to fall back to the default, you need to explicitly pass undefined in it’s place, as shown below:

const totalBill = calculateBill(100, undefined, 0.25);

One caveat to keep in mind is that this is order-dependent (without using destructuring).

At the time of publishing this post, this feature has been around for a while and has pretty solid browser support.

References & Resources