Member-only story
Javascript Interview Questions: How to use default function parameters (EcmaScript 6)
Javascript Interview Questions # 4
How can we use default function parameters?
Hello everyone, in this tutorial we will learn about how to use default parameters in javascript functions with examples.
With the help of ES6 in the javascript we can start using default parameters in functions. With the help of setting default values to parameters helps a lot regarding to function usages.
Functions with default parameters
Lets have a look how we can define functions with default parameters.
Lets create a multiply function with default function parameters.
const multiply = (x=3,y=4) => {
return x*y
}sum(10,5) // 50
sum(10) // 40
sum() // 12
As you see in the example above we have defined x = 3 and y = 4 as their default values.
For the first example when we pass x as 10 and y as 5 that ignores the default values and uses as x=10 and y=5
When we only pass the first argument as 10, y has been used as the default assigned value. In that case x=10 and y=4