One of the features added in ES6 is the ability to use default parameters. Default parameters allow parameters to have a predetermined value in case there is no argument passed into the function or if the argument is undefined when called.
Take a look at the code snippet below that uses a default parameter:
- In the example above, we used the
=operator to assign the parameternamea default value of'stranger'. This is useful to have in case we ever want to include a non-personalized default greeting! - When the code calls
greeting('Nick')the value of the argument is passed in and,'Nick', will override the default parameter of'stranger'to log'Hello, Nick!'to the console. - When there isn’t an argument passed into
greeting(), the default value of'stranger'is used, and'Hello, stranger!'is logged to the console.
By using a default parameter, we account for situations when an argument isn’t passed into a function that is expecting an argument.