Skip to content

Function default parameter value

Starting from ES2015 JavaScript update, you can set default value to your function parameters using the following syntax:

1
2
3
4
5
6
7
8
function myFunc(x = 10) {
  return x;
}
console.log(myFunc()) // 10 -- no value is provided so x default value 10 is assigned to x in myFunc
console.log(myFunc(5)) // 5 -- a value is provided so x is equal to 5 in myFunc

console.log(myFunc(undefined)) // 10 -- undefined value is provided so default value is assigned to x
console.log(myFunc(null)) // null -- a value (null) is provided, see below for more details

The default parameter is applied in two and only two situations:

  • No parameter provided
  • undefined parameter provided

In other words, if you pass in null the default parameter won't be applied.

Note: Default value assignment can be used with destructured parameters as well (see next notion to see an example)

External resource