Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Default parameters

A default parameter uses its initializer when an argument is omitted or explicitly passed as undefined. Values such as null, 0, false, and an empty string do not trigger the default.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

function name(parameter = expression) { /* body */ }

A default parameter has an initializer written as parameter = expression in a function's parameter list. The expression supplies a value when the corresponding argument is omitted or has the value undefined.

Values null, 0, false, and "" remain unchanged. This is not equivalent to parameter || fallback, which also replaces other falsy values. A default value does not validate the argument's type either.

Initializers are evaluated at call time, only when needed, from left to right. A later parameter can use an earlier one, as in label = value. Reading a later parameter that has not yet been initialized throws ReferenceError. An initializer such as [] or {} creates a new object each time it is evaluated.

Initializers cannot access local declarations in the function body. Put helper values in an accessible outer scope or compute them inside the body. The example demonstrates the difference between missing and explicit values, as well as the dependency of label on value.

Examples

function describe(value = "guest", label = value) {
  return JSON.stringify([value, label]);
}

console.log(describe());
console.log(describe(undefined));
console.log(describe(null));
console.log(describe(0));
console.log(describe(false));
console.log(describe(""));

Result

["guest","guest"]
["guest","guest"]
[null,null]
[0,0]
[false,false]
["",""]

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources