Skip to main content
Default value destructuring is a syntax mechanism in JavaScript that assigns a fallback expression to a variable during object or array destructuring. This fallback is evaluated and assigned exclusively when the targeted property or element in the source structure is strictly undefined or does not exist.

Trigger Condition

The default value is only applied if the extracted value is strictly equal to undefined (value === undefined). Falsy values such as null, 0, false, or "" (empty string) are considered valid values and will bypass the default assignment.

Object Destructuring

In object destructuring, the default value is assigned using the assignment operator (=) immediately following the property identifier.

Array Destructuring

In array destructuring, defaults are applied based on the iterable’s positional indices. Out-of-bounds indices yield undefined, thereby triggering the default value.

Aliasing with Default Values

When renaming a variable during object destructuring (aliasing), the default value assignment must follow the new identifier. The syntax pattern is sourceProperty: targetVariable = defaultValue.

Lazy Evaluation

Default value expressions are evaluated lazily. The expression on the right side of the = is only executed if the default value is actually required.

Nested Destructuring with Defaults

Default values can be applied at multiple levels of a nested destructuring assignment. To prevent a TypeError when attempting to destructure properties from an undefined parent, a default empty object ({}) is often assigned to the parent level.
Tired of Poor JavaScript Skills? Fix That With Deep Grasping!Learn More