Home > database >  Set a parameter Value of Function Js
Set a parameter Value of Function Js

Time:12-11

PROBLEM => I WANT TO ADD VALUE OF PARAM CAPACITY BUT I WANT ALSO NAME PARAM TO BE DEFAULT

function Js(name = 'Mango' , capacity = 40){
console.log(`${name} with a capacity ${capacity} will be delivered !!!`)
}
Js() // .... Correct
Js("Something" , 40) // .... Also Correct

But What if i want to just give value to capacity and set name default

Like this =>

Js( ..., 55) // Mango with a capacity 55 will be delivered !!!

CodePudding user response:

Put default argument on second place.

function Js(capacity, name= 'Mango'){
console.log(`${name} with a capacity ${capacity} will be delivered !!!`)
}
Js(40) //"Mango with a capacity 40 will be delivered !!!"
Js(40, 'Something else') // "Something else with a capacity 40 will be delivered !!!"
  • Related