Emmanuel Akinfulubi

JavaScriptBasic

Default parameters in JavaScript functions

Beginner · 4 min read

Most function calls use the same settings most of the time. A fare calculator nearly always uses the standard rate per kilometer, so it is tiring to pass that rate on every call. A default parameter lets you write the usual value once, in the function itself, and the caller only passes it when they want something different.

In this tutorial you will write fare(distanceKm, ratePerKm, surcharge) where the rate and the surcharge have defaults, and the surcharge default is worked out from the rate. Then you will find out exactly what makes a default kick in, and why null doesn't. You need to know how to write and call a function.

What a default looks like

You write = value after a parameter name:

JavaScript

function greet(name = 'friend') {
  return 'Hello, ' + name;
}
console.log(greet('Bola')); // Hello, Bola
console.log(greet());       // Hello, friend

When the caller leaves name out, it would normally be undefined. The default fills that gap.

The exact rule

A default is used when the argument is undefined, and only then. Leaving an argument off counts, because a missing argument is undefined. Passing undefined on purpose counts too. Every other value, including null, 0 and '', is used as it is.

Here is the exercise. Try it yourself first if you like, or read on and we will build it together.

89 Default parameters, and what triggers them

easy

Write a fare calculator whose rate and surcharge have defaults, where the surcharge default is worked out from the rate. Then call it with undefined and with null and explain the difference.

Stuck? Three hints, smallest first
  1. 1. A default fires when the argument is undefined, including when it is left off.
  2. 2. null is a value, so it is passed through and coerced.
  3. 3. A later default can refer to an earlier parameter, because they are evaluated left to right.

Step 1: a default rate

Amounts are in kobo (100 kobo make one naira). 50_00 is 5,000 kobo, or ₦50 per kilometer; the underscore only makes it easier to read.

Step 1

function fare(distanceKm, ratePerKm = 50_00) {
  return distanceKm * ratePerKm;
}
console.log(fare(4));         // 20000
console.log(fare(4, 100_00)); // 40000

With no rate, the default ₦50 is used. With a rate, that rate wins.

Step 2: a default built from another parameter

The surcharge should be a tenth of the rate unless the caller says otherwise. A default can use any parameter to its left:

Step 2

function fare(distanceKm, ratePerKm = 50_00, surcharge = ratePerKm / 10) {
  return distanceKm * ratePerKm + surcharge;
}
console.log(fare(4));         // 20500
console.log(fare(4, 100_00)); // 41000

Defaults are worked out from left to right, each time the function is called. By the time JavaScript reaches surcharge, ratePerKm already has its value, whether it came from the caller or from its own default. So when the rate goes up, the surcharge follows: 5,000 becomes 10,000, and its tenth becomes 1,000.

Step 3: skip a middle argument with undefined

What if you want the default rate but no surcharge? You can't leave out the middle argument, but you can pass undefined to ask for its default:

Step 3

function fare(distanceKm, ratePerKm = 50_00, surcharge = ratePerKm / 10) {
  return distanceKm * ratePerKm + surcharge;
}
console.log(fare(4, undefined, 0)); // 20000

The rate falls back to ₦50. The surcharge is 0, which is a real value, so it is used. The default would have been 500.

Step 4: null is not undefined

Here is the surprise. Pass null for the rate:

Step 4

function fare(distanceKm, ratePerKm = 50_00, surcharge = ratePerKm / 10) {
  return distanceKm * ratePerKm + surcharge;
}
console.log(fare(4, null)); // 0

null is a value, so the default does not fire. ratePerKm is null, 4 * null turns null into 0, the surcharge is null / 10, also 0, and the trip is free. No error anywhere. null often comes from a database or an API meaning "not set", so this happens in real code. If you want null treated as missing too, write ratePerKm ?? 50_00 inside the function instead.

Step 5: a fresh array on every call

A default is worked out again on every call. That makes a default like [] safe:

Step 5

function addStop(stop, route = []) {
  route.push(stop);
  return route;
}
console.log(addStop('yaba'));  // [ 'yaba' ]
console.log(addStop('ikeja')); // [ 'ikeja' ]
const shared = [];
addStop('yaba', shared);
addStop('ikeja', shared);
console.log(shared); // [ 'yaba', 'ikeja' ]

The two calls without a route each got a new, empty array, so 'ikeja' didn't land next to 'yaba'. When you pass your own array, the function adds to it, because it is your array.

Step 6: length stops at the first default

A function has a length property, the number of parameters it expects. It only counts the ones before the first default:

Step 6

function fare(distanceKm, ratePerKm = 50_00, surcharge = ratePerKm / 10) {
  return distanceKm * ratePerKm + surcharge;
}
console.log(fare.length); // 1

So don't use length to find out how many arguments a function accepts.

The full solution

The answer panel has fare with both defaults and addStop with a default array, and checks the defaults, the passed rate, undefined, null, fare.length and the fresh array per call. Press Run to see the output.

The answer

function fare(distanceKm, ratePerKm = 50_00, surcharge = ratePerKm / 10) {
  return distanceKm * ratePerKm + surcharge;
}

// A default expression runs on every call, so a fresh array each time.
function addStop(stop, route = []) {
  route.push(stop);
  return route;
}

const shared = [];
addStop('yaba', shared);
addStop('ikeja', shared);

console.log('defaults:', fare(4), '| own rate:', fare(4, 100_00), '| null rate:', fare(4, null));
console.log('fare.length is', fare.length);

Output


      
8 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('both defaults fill themselves in', fare(4), 20500);
test('a passed rate is used, and the surcharge follows it', fare(4, 100_00), 41000);
test('undefined asks for the default', fare(4, undefined, 0), 20000);
test('null does not: it is a value, and it coerces to zero', fare(4, null), 0);
test('length counts only the parameters before the first default', fare.length, 1);
test('a default array is built fresh on every call', addStop('yaba'), ['yaba']);
test('so the second call cannot see the first', addStop('ikeja'), ['ikeja']);
test('an array you passed in is shared, because you own it', shared, ['yaba', 'ikeja']);

Only undefined triggers a default. fare(4, null) passes null straight through, 4 * null is 0, and the answer is a fare of nothing with no error anywhere — the single most common surprise with defaults, and the reason ?? exists (exercise 98).

Defaults are evaluated left to right at call time, which buys two things. A later default can be computed from an earlier parameter, as surcharge is here. And a default of [] or {} is built afresh on every call, unlike Python, where a mutable default is created once and remembered — a whole class of bug that JavaScript simply does not have. Note also that fare.length is 1: length stops counting at the first default, so it is not a reliable arity.

Common mistakes

  • Expecting null to trigger a default. Only undefined does. Use ?? inside the function if null should count as missing.
  • Using a parameter in a default before it exists. A default can only use parameters to its left.
  • Writing rate = rate || 50_00 in the body. That also replaces a real 0. A default parameter or ?? does not.

Try it yourself

  1. Add a fourth parameter, currency = '₦', and return text like ₦205.00 instead of kobo.
  2. Change fare so that fare(4, null) uses the default rate. Use ?? in the body.
  3. Write makeTicket(name, seat = 'any', time = new Date().getHours()) and call it twice. Is the hour worked out once or on every call?

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.