Parameters are like placeholders for data that will be passed to a function.
Arguments are the actual values passed to a function while calling it
functioncalculateBill(billAmount, taxRate){// here billAmount, taxRate are parametersconst total = billAmount + billAmount * taxRate
return total;}calculateBill(100,0.13);// here 100, 0.13 are arguments
Parameters are variables local to the function; available only inside the function.
You can also pass variables as arguments during a function call.
We can also pass expressions as arguments to a function.
myTotal3 =calculateBill(20+20+30,0.3);
So, we can either pass direct value or variables holding value or expressions resulting in a value to a function as arguments.
Passing functions as arguments:
functiondoctorize(name){return`Dr. ${name}`;}functionyell(name){return`HEY ${name.toUpperCase()}`;}// We can pass a function inside anotheryell(doctorize('Soumya'));// HEY DR. SOUMYA// Above, returned value of doctorize function is passed to yell function
Default values:
functionyell(name ='Silly Goose'){return`HEY ${name.toUpperCase()}`;}yell('Soumya')// HEY SOUMYAyell()// HEY SILLY GOOSE// Above, if we don't pass any argument to yell function, then it takes the default value in function definition,// here Silly Goose, else it takes whatever we pass as argument.
Important gotcha:
functioncalculateBill(billAmount, taxRate =0.13, tipRate =0.15){
console.log('Running Calculate Bill!!');const total = billAmount + billAmount * taxRate + billAmount * tipRate;return total;}// Suppose above, we want to pass the tipRate but not the taxRate and want taxRate to be default,// then the only thing we can do is:calculateBill(100,undefined,0.66);// here the taxRate will default to 0.13 as // we have passed undefined to it and the tipRate will be 0.66 as passed