onyeePeace

onyeePeace

Default Parameters

Description

It allows you to use some backup values for parameters when they are not explicitly passed through into a function where they are required.

Example:

function cake(size, flavour, icing) {
    console.log(`I want a ${size} inch ${flavour} cake with ${icing} decorations.`)
}
cake();
//Outputed result will be undefined as no argument was passed when calling the function

//You can add default parameters like so;
function cake(size=10, flavour="chocolate", icing="buttercream") {
    console.log(`I want a ${size} inch ${flavour} cake with ${icing} decorations.`)
}
cake();
//Outputed result will be; "I want a 10 inch chocolate cake with buttercream decorations."

//If an argument is passed when calling the function, it will overide the default parameter like so;
function cake(size=10, flavour="chocolate", icing="buttercream") {
    console.log(`I want a ${size} inch ${flavour} cake with ${icing} decorations.`)
}
cake(6, "vanilla", "fondant");
//Outputed result will be; "I want a 6 inch vanilla cake with fondant decorations."
 
Share this