Default parameters

Values a function parameter gets when the argument is not passed or is undefined.

On this page

Example

#
JavaScript
function createPlayer(name = 'Guest', level = 1) {
  return `${name}, level ${level}`
}

console.log(createPlayer('Emma', 5))
console.log(createPlayer('Jack'))
console.log(createPlayer())
Console
Emma, level 5
Jack, level 1
Guest, level 1

Definition and usage

#

You write a default parameter in the function definition with an equals sign: function greet(name = "Guest"). If you omit the argument at the call site, or pass undefined, the parameter gets the default value. That way the function doesnโ€™t have to check for itself whether it received the data.

The default value only kicks in for undefined. Passing null, 0, false, or an empty string is a deliberate choice of value, so itโ€™s used as is. Thatโ€™s what sets default parameters apart from the older name = name || "Guest" pattern, which also replaced valid data, like zero.

The default expression is evaluated on every call where itโ€™s needed, not once when the function is defined. So every call with items = [] gets a new, empty array. A default value can use earlier parameters, e.g. (points, bonus = points / 2), and it combines with destructuring: function setup({ theme = "dark" } = {}).

Syntax

#
Syntax
function name(param = defaultValue) {}
const fn = (a, b = a * 2) => {}
function name({ key = value } = {}) {}

More examples

#
Only undefined triggers the default value
JavaScript
function setVolume(volume = 50) {
  return volume
}

console.log(setVolume(undefined))
console.log(setVolume(null))
console.log(setVolume(0))

function oldSetVolume(volume) {
  volume = volume || 50
  return volume
}

console.log(oldSetVolume(0))
Console
50
null
0
50

The old pattern with || turned the valid volume 0 into 50. The default parameter left the zero alone, because it only reacts to undefined.

A value that depends on an earlier parameter
JavaScript
function createTask(title, points = 10, bonus = points / 2) {
  return { title, points, bonus }
}

console.log(createTask('Quiz'))
console.log(createTask('Project', 40))
Console
{ title: 'Quiz', points: 10, bonus: 5 }
{ title: 'Project', points: 40, bonus: 20 }
An options object with default values
JavaScript
function startLesson({ speed = 1, subtitles = true } = {}) {
  return `Speed ${speed}x, subtitles: ${subtitles ? 'on' : 'off'}`
}

console.log(startLesson({ speed: 1.5 }))
console.log(startLesson())
Console
Speed 1.5x, subtitles: on
Speed 1x, subtitles: on

The trailing = {} means the function can be called with no argument. Without it, destructuring undefined would throw a TypeError.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Put parameters with default values at the end of the list. Then you only need to omit the last arguments, instead of passing undefined in the middle.
  • For an options object, add = {} for the whole parameter. Without it, calling the function with no argument throws a TypeError during destructuring.
  • When you also want to replace null, use the ?? operator in the function body: const size = input ?? 16.

Related entries

#

Spotted a mistake or missing an example? Write to us.