Default parameters

Default parameters

·

3 min read

Overview

Default parameters are one of the feature introduced in ES6 (the 6th version of ECMAScript). By using this feature developers can initialize a function parameter with a default value, if no value or “undefined” is passed as an argument to a function.

Introduction

Let's understand this concept generically, for example in all our smartphones we have a themes feature, there are default themes and also you can customize your theme based on your choice. The default theme has some fonts, background images, the color of text, etc… if you don’t like any of these you can change them, you can set your own choice of fonts, the color of text, etc… and if you don’t make any changes the default theme will apply.
In Javascript also "default parameter" is similar to that.

What is the default parameter?

In Javascript, we have a feature through ES6 that while declaring the function, you can initialize a parameter with a default value, if no value or “undefined” is passed, the logic we defined in the function body will consider that default parameter value.
A default parameter can be anything from a number to another function.

Here is an example:

As you can see in the above example,

  1. We initialized variable b with a value in the function definition, it’s called the default parameter.

  2. In the first console, we are getting output as 2, we passed the value to a only, not to b, but in the function body, the operation we are performing will consider the default value for b and return the desired output.

  3. If you pass the value to a and b while calling a function, the value passed to b will be set, it won’t consider the default value, so here comes we can change the value to the parameter as per our needs.

How default parameter is useful?

In the past, the generic strategy for setting default value was to test parameter value in the function body and assign a value if they are “undefined”.

In the following example, b is set to 1 if multiply is called with only one argument:

But with the default parameter, we can set the default value directly in the function definition rather than checking for it in the function body.
Example:

By using the default parameter we can write a more concise, less error-prone and easier-to-read function.

One more use-case you have to consider here is:
The following example will result in a syntax error

Because a comma without a value between 1 and 3 is not a valid syntax in javascript. ”Default parameter only starts working from the right side.” So, to avoid the syntax error, you should either provide a value for b or remove the comma and pass the arguments as separate values.

Summary

  1. The default parameter was introduced in ES6.

  2. Before the default parameter was introduced, we used to test parameter value in the function body and assign a default value if they are “undefined”.

  3. By using the default parameter, we can set the default value directly in the function definition.

  4. The default parameter only starts working from the right side.

Conclusion

Default parameters help us to write a more concise, easier to read and less-error-prone function.
Thank you for reading 😊. Share it if you derived some value from the blog.

#