JavaScript Functions
A function is a reusable block of code. You give it inputs, it does some work, and it can give a value back with return.
function addTax(price, rate) {
const tax = price * rate;
return price + tax;
}
const total = addTax(100, 0.1);
console.log(total);Expected output
110
Common mistake: Forgetting return
Without return the function gives back undefined, so total would be undefined instead of 110.