Functions
A function lets you write logic once and use it as many times as you want. This note covers declarations, parameters and return, the three ways to write a function, hoisting differences, default and rest parameters, and how arrow functions differ from normal ones.
5.1 Why is it used?
- Adding 18% GST to three products means the same calculation copy pasted three times.
- When the rate changes you have to edit all three places.
- Forget even one and that product shows a wrong price: a hard-to-find bug.
- A function: write the logic once, call it as many times as you want.
JavaScript
const shoes = 1000;const shoesTotal = shoes + (shoes * 18) / 100;console.log(shoesTotal); const bag = 500;const bagTotal = bag + (bag * 18) / 100;console.log(bagTotal); const watch = 2000;const watchTotal = watch + (watch * 18) / 100;console.log(watchTotal);Output
> 1180> 590> 23605.2 Function declaration, parameter and return
- function keyword + name (addGst) + parameters in brackets (price is the input).
- return sends the result (price + tax) back out of the function.
- Defining a function does not run it; it runs only when you call it.
- addGst(1000): 1000 is the argument, it goes into the price parameter, and 1180 comes back.
- If the GST rate changes, you update only one line.
JavaScript
function addGst(price) { const tax = (price * 18) / 100; return price + tax;} console.log(addGst(1000));console.log(addGst(500));console.log(addGst(2000));Output
> 1180> 590> 2360- Step 1
1.Call addGst(1000)
- Step 2
2.The argument 1000 goes into the parameter price
- Step 3
3.Body runs: tax = 1000 * 18 / 100 = 180
- Step 4
4.return 1000 + 180 sends 1180 back
- Step 5
5.console.log prints 1180
5.3 3 ways to write a function
- Declaration: function double(n) { ... }
- Expression: a function stored in a const variable.
- Arrow function: no function keyword; for a single expression drop the braces and return (implicit return).
- In JavaScript a function is a value you can keep in a variable.
JavaScript
// 1. Declarationfunction double(n) { return n * 2;} // 2. Expressionconst triple = function (n) { return n * 3;}; // 3. Arrow functionconst square = (n) => n * n; console.log(double(4), triple(4), square(4));Output
> 8 12 165.4 Checkpoint: call first, define later
- sayHi is a declaration: it is fully hoisted, so calling it before its line works.
- sayBye is a const arrow function: it cannot be used before its line, so ReferenceError.
- Details come in the hoisting video.
JavaScript
console.log(sayHi("Riya"));console.log(sayBye("Riya")); function sayHi(name) { return `Hi ${name}`;} const sayBye = (name) => `Bye ${name}`;Output
> Hi RiyaReferenceError: Cannot access 'sayBye' before initialization5.5 Default parameter
- Not every product has 18% GST; some have 5%.
- rate = 18 gives a fallback when the argument is skipped.
- addGst(1000) uses 18 (1180); addGst(1000, 5) uses 5 (1050).
5.6 Rest parameter
- ...prices collects any number of arguments into one array.
- cartTotal(199, 499, 99) loops over the array and returns 797.
JavaScript
function addGst(price, rate = 18) { return price + (price * rate) / 100;} console.log(addGst(1000));console.log(addGst(1000, 5)); function cartTotal(...prices) { let total = 0; for (const p of prices) total += p; return total;} console.log(cartTotal(199, 499, 99));Output
> 1180> 1050> 7975.7 Comparison: arrow vs normal function
| Point | Normal function | Arrow function |
|---|---|---|
| Syntax | function name(a) { return ... } | (a) => ... (implicit return) |
| Own this | Yes (depends on how it is called) | No, uses the outer this |
| arguments object | Yes | No |
| Use with new | Yes (constructor) | No |
| Hoisting | Declaration: fully hoisted | Behaves like a variable (const) |
| Best for | Object methods (shorthand) | Short callbacks |
5.8 Common mistakes
- Arrow function with curly braces but no return: the result is undefined.
- Fix: remove the braces (implicit return) or write return.
JavaScript
// Wrongconst double = (n) => { n * 2;};console.log(double(5));JavaScript
// Rightconst double = (n) => n * 2;console.log(double(5));- Using an arrow function as an object method: it has no own this, so this is not the user and the name is not found.
- Fix: method shorthand greet() { ... }, then this is the user and the output is Hi Aman.
JavaScript
// Wrongconst user = { name: "Aman", greet: () => `Hi ${this.name}`,};console.log(user.greet());JavaScript
// Rightconst user = { name: "Aman", greet() { return `Hi ${this.name}`; },};console.log(user.greet());5.9 When to use which
- Object methods: method shorthand.
- Short callbacks (map, filter, setTimeout): arrow functions.
- One function, one job, with a name that says what it does (addGst, cartTotal).
5.10 Interview tips
- Arrow functions give short syntax and implicit return for a single expression.
- They have no own this, arguments or prototype, so they cannot be used as constructors.
- Function declarations are fully hoisted; arrow and expression functions behave like variables.
- Parameter = the name in the definition; argument = the value passed in the call.