Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

default parameter scope

Default parameter initializers run in their own scope, outside the function body. Earlier parameters are visible, but declarations in the body are not and can cause a runtime ReferenceError.

How it works

A default parameter initializer is evaluated when its argument is omitted or undefined. Initializers have a separate scope outside the function body: earlier parameters are available, but body declarations are not, so referencing one raises ReferenceError.

Syntax

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

Key points

  • Default initializers execute in a dedicated scope positioned outside the function body's scope.
  • A parameter may use an earlier parameter as its fallback value.
  • Function-body declarations are not visible from parameter initializers.
  • Using an inaccessible body declaration in an initializer produces a runtime ReferenceError.
  • The initializer is evaluated when the function is called, rather than when the function is defined.

Common mistakes

  • Declarations in the function body are unavailable to default initializers, including function declarations and var variables; referencing them throws ReferenceError.
  • The initializer scope is distinct from and parent to the function-body scope, which explains why body declarations cannot be reached.

Example: Default parameter scope

function greet(name = makeName()) {
  return `Hello, ${name}`;
  function makeName() {
    return "Ada";
  }
}

try {
  console.log(greet());
} catch (error) {
  console.log(error.name);
}

The initializer cannot access declarations in the function body, so calling greet without an argument throws ReferenceError.

When to use it

Dependent defaults

Derive a missing argument from a preceding parameter, such as constructing a label from an earlier value.

Diagnosing scope errors

Recognize why a helper declared inside the body cannot be called from a parameter fallback.

Interview questions

QWhat scope do default parameter initializers use?

Default parameter initializers run in a scope separate from the function body. That scope is the parent of the body scope, so body declarations are not visible while an initializer is evaluated.

Follow-up: Can an initializer use an earlier parameter? Yes. Parameters are processed left to right, so an earlier parameter is available to a later initializer.

QCan one default parameter depend on another?

Yes, an initializer may refer to parameters declared earlier in the parameter list. Parameters are initialized from left to right, making those earlier bindings available.

Follow-up: What happens if it refers to a later parameter? The later parameter has not been initialized yet, so the reference fails during evaluation.

QWhy can a body function not be used in a default initializer?

A function or variable declared in the function body cannot be referenced by a default initializer. Such a reference causes a runtime ReferenceError, including references to var declarations.

Follow-up: Can the initializer call a function declared outside the function? Yes, outer-scope bindings remain accessible through normal lexical scope lookup.

  • default parameters
  • function body
  • function scope
  • parameter initializers
  • ReferenceError

Source: Default parameters. Checked 2026-10-15.