ES6: Default Parameters function

Often when we define a function we either have to supply a default or check if the value is passed and if not will either report back or throw. In ES6 you could supply a default parameters inside the function in case you want to use a default or to catch bug.

This is how you used to do it in < ES6

function example(first, second, third) {  
   first = first || 'default value 1';
   second = second || { test: 'value' };
   third = third || 10;
   console.log(first, second.test, third);
}

example();  
// will log: default value 1 value 10
example('hello', {test: 'string'});  
// will log: hello string 10

In ES6 you can do:

function example(first='default value 1', second={test: 'value'}, thrid=10) {  
   console.log(first, second.test, third);
}

example();  
// will log: default value 1 value 10
example('hello', {test: 'string'});  
// will log: hello string 10

Check out MDN for Default parameters for more information here.

If you have any question or you think this post can be optimized in anyway please feel free to tweet me @alicoding