JavaScript: functions
A function groups reusable instructions and can receive parameters and return a result.
What you will learn
- How to define and call a function
- How parameters and return values work
- How local variables avoid accidental shared state
Minimal example
function addTax(price, rate) {
return price * (1 + rate);
}
console.log(addTax(100, 0.1)); // 110Define behavior once and call it with different arguments. A return value can be stored, displayed, or passed to another function.
Common mistakes
- Do not confuse a parameter in the definition with an argument at the call site.
- Return a value explicitly when the caller needs a result.
- Keep side effects clear and avoid relying on unrelated global variables.