What is default parameter in JavaScript
Default parameter return itself when the function don't have passing any argument which is need to perform the task by function.
It return the value when not any parameter is passing through the function as an argument and help to show the error.
Example 1: Predefined default value printing and also to pass the argument
Example 2: Fixed Predefined default value printing and also to pass the argument which will arise error
function activity (name = 'Default Name', act = 'Default Act') {
return `${name} loves ${act}`
}
console.log(activity('sam', 'blogging')) // sam loves blogging
console.log(activity()) // Default Name loves Default Act
var dPerson = {
name: { fn: 'Sam', ln: 'blogging' },
act: 'See'
}
function lAct (p = dPerson) {
return `${p.name.fn} loves ${p.name.ln}`
}
console.log(lAct()) // Default Name loves Default Act
console.log(lAct('sam', 'blogging')) // TypeError