Consider the following snippet of code.
function sum(a, b)
a = typeof a !== 'undefined' ? a : 42;
b = typeof b !== 'undefined' ? b : 8;
In the above code we have a function called
sum which takes two arguments
b. Note that we are calling
sum() on line 7 without passing any arguments. On line 3 and 4 checking if the type of the variables is
undefined or not using the ternary operator. I am using the ternary operator but this can be done in various other ways.
If we find that the values of the variables is
undefined then we are setting default values
As you can see this adds a few extra lines and can make the code difficult to read.
function sum(a=42, b=8)
With ES6 we can set the default parameters right at the top inside the parenthesis along with our variables. This is not only easy to read but it make out code a few lines shorter which is always nice.