Default values ​​of function parameters

Default values ​​of function parameters

Introduction

In previous practice (ES5) the following approach was used to define the default values of funkcje parameters:

The problem occurs when a value is passed that the compiler implicitly converts to a boolean “false” (eg zero), and therefore takes the default value instead:

So we need to improve the condition:

ES2015 syntax

With the new standard came a simpler syntax for defining default values with the following rule:

The default defined value is applied in case the argument is missing or undefined and “null” is a valid value!

default function parameters

Defining a parameter default value also works well in conjunction with destructuring:

Besides the aforementioned missing value or “undefined”, the default value can be anything else, and even another function: