Default parameters let you give a function parameter a fallback value, written as function greet(name = "friend"). The default is used when the argument is missing or is undefined. This guide explains how javascript default parameters are evaluated, how they differ from the older || trick, and the pitfalls to avoid.
Default parameters were added in ES2015 and are supported everywhere today. They remove the need for manual checks at the top of a function.
Write an equals sign and the default value after the parameter name.
When you call greet() with no argument, name is undefined, so the default applies. When you pass a value, the default is ignored.
The default triggers only for undefined. Other falsy values are treated as real arguments.
This is the main difference from the old pattern value = value || "default", which replaces 0, "" and false as well. Defaults are safer when zero or an empty string is a valid input.
The default expression runs each time the function is called, not once when it is defined. That means a default of [] or {} is a fresh object per call, unlike the mutable default trap in some other languages.
The counter increments only when the default is actually used, so the call with 10 does not touch it.
A default can refer to parameters on its left, and it can call functions.
A default cannot use a parameter that comes later. Doing so throws a ReferenceError because that parameter is still in its temporal dead zone.
A parameter with a default can sit before one without, but then the caller must pass undefined explicitly to use the default.
Put parameters with defaults last to avoid this awkward call. You can also make a parameter required by defaulting it to a function that throws.
For functions with many options, destructure an object parameter and give each field a default. Add = {} so the function can be called with no argument.
Without the final = {}, calling connect() would throw a TypeError, because you cannot destructure undefined.
When a parameter has a default value, the function is in non-simple parameter mode. In that mode the arguments object no longer tracks changes to the named parameters, and a function with defaults cannot contain a "use strict" directive. In practice, use rest parameters and defaults together in modern code and ignore arguments.
No. Only undefined triggers a default. If null may arrive, handle it with ??, as in value ?? "default".
On every call where the argument is missing. A default array or object is created fresh each time.
|| replaces any falsy value, including 0 and empty string. A default parameter replaces only undefined.
Yes. The length property counts only the parameters before the first one with a default value.
Default parameters give a parameter a fallback value when the argument is undefined. They are evaluated per call, can use earlier parameters, and are safer than the || pattern. Put them last, use destructuring with = {} for option objects, and use ?? when null must also fall back.