JavaScriptFunctionsBeginner

Default parameters

Sets fallback values for missing or undefined parameters.

Review the syntaxStudy the examplesOpen the coding app
function greet(name = 'there') { ... }

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Sets fallback values for missing or undefined parameters.

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed. Unlike the ES5 pattern using the logical OR operator (||), which triggers on any falsy value (0, '', false), default parameters specifically check for undefined. This prevents the 'falsy value bug' where passing 0 would accidentally trigger a default value of 1. Performance-wise, default parameters are evaluated at call time, meaning a new instance of an object or a new result of an expression is created for every invocation that relies on the default. Furthermore, parameters are evaluated from left to right, allowing later parameters to reference earlier ones, though referencing a parameter to its right results in a ReferenceError due to the Temporal Dead Zone.

Quick reference

Syntax

function greet(name = 'there') { ... }

See it in practice

Examples

1

Basic Parameter Initialization

function multiply(a, b = 1) {
  return a * b;
}

console.log(multiply(5, 2));
console.log(multiply(5));
Output:
10 5

If the second argument is omitted or passed as undefined, the default value of 1 is used.

2

Referencing Previous Parameters

function calculateTotal(price, tax = price * 0.1) {
  return price + tax;
}

console.log(calculateTotal(100));
console.log(calculateTotal(100, 20));
Output:
110 120

Parameters defined earlier in the list are available to subsequent default expressions.

3

Dynamic Default Values via Functions

function getInitialValue() {
  return 10;
}

function add(a, b = getInitialValue()) {
  return a + b;
}

console.log(add(5));
Output:
15

A function call can be used as a default value. It is executed only when the argument is missing.

Debug faster

Common Errors

1

LogicError

Cause: Passing null does not trigger the default parameter value because null is considered a defined object value in JavaScript.

Fix: Pass undefined instead of null if you want the default value to be used, or add a manual check within the function body.

function test(val = 'default') { return val; }
test(null); // returns null, not 'default'
2

ReferenceError

Cause: Attempting to use a parameter as a default value before it has been initialized (forward-referencing).

Fix: Ensure that default expressions only reference parameters defined to their left.

function errorFunc(a = b, b = 5) { return a + b; }
errorFunc(); // ReferenceError: Cannot access 'b' before initialization

Runtime support

Compatibility

Node.js, BrowserAll modern browsers, IE not supportedES6 (2015)

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Sets fallback values for missing or undefined parameters.

LogicError: Pass undefined instead of null if you want the default value to be used, or add a manual check within the function body. ReferenceError: Ensure that default expressions only reference parameters defined to their left.