ES6 handling default parameters

Handling default values for parameters in method signatures are important to make your code flexible and also to handle unexpected errors on missing parameters.

In ES5 you could do something like below

In ES6, with the introduction of default parameters, the above code can be refactored like below

}

The default parameter sets the page = 1 and limit = 20 if they aren’t passed in to the function. This makes our code more self-descriptive.

Now, how about objects passed in as parameters? In ES5,

}

In ES6, we can specify the default object using default parameters in the method signature

}

Using default parameters makes our code more descriptive and avoids manual conditional checks in the body of the method.

Front-end developer

There frontend developers can share their experience with other developers

Ashwin Sureshkumar

Written by

Front-end developer

There frontend developers can share their experience with other developers