Default Parameter - 03

Default parameters:

Default parameters have a default value or predefined value which passes into a function when there is no argument passed into the function or if the argument is undefined when called.

function greeting (name = 'stranger') {
    console.log(`Hello, ${name}!`)
}


greeting('Nick') // Output: Hello, Nick!
greeting() // Output: Hello, stranger!

In the example above, a name is a default parameter that has a default or predefined value ‘stranger’. When argument ‘Nick’ passes in, then the argument overrides the default parameter value or predefined value ‘stranger’ and executes the code inside the function body. 


When there is no argument or undefined argument passed into function greeting(), then the default value ‘stranger’ or predefined value of the default parameter is used and executed or run inside the function body.


Post a Comment

Previous Post Next Post