JavaScript: Default parameters
1 min read

JavaScript: Default parameters

In JavaScript, default parameters are evaluated when the function is called instead of at definition time, as in Python. This opens up a lot of flexibility.

function multiply(x, y=x){
    return x * y;
}
multiply(5) // prints 25

In the above example, the y parameter gets the value from x as the default parameter is evaluated when the function is called rather than when the function is defined.