Functions
Declarations, expressions, arrow functions, default and rest parameters, spread and callbacks.
Functions package logic so you can reuse it, name it and test it. They're the most important building block in JavaScript — and in JavaScript, functions are also values: you can store them in variables, pass them to other functions and return them.
Function declarations#
aandbare parameters — placeholders in the definition.2and3are arguments — the actual values you pass in.returnsends a value back and immediately exits the function. A function withoutreturnreturnsundefined.
Declarations are hoisted: you can call them above the line where they're written.
Function expressions#
A function can also be created as a value and assigned to a variable:
Function expressions are not hoisted — calling multiply before this line throws a ReferenceError.
Arrow functions#
Arrow functions are a shorter syntax, and the most common style for small functions and callbacks:
Arrow functions differ from regular functions in a few ways: they don't have their own this or arguments, and they can't be used with new. That makes them ideal for callbacks, while object methods that use this should use regular method syntax. The this lesson explains why.
Default parameters#
Defaults apply when the argument is missing or undefined — but not for null.
Rest parameters#
... in a parameter list gathers "the rest" of the arguments into a real array:
The rest parameter must be last. It replaces the old arguments object, which isn't a real array and doesn't exist in arrow functions.
Spread arguments#
Spread is the mirror image of rest: it expands an array into individual arguments.
Functions as values: callbacks#
Because functions are values, you can pass one into another. A function passed this way is a callback:
You'll use callbacks constantly — with array methods, timers and events:
A function that takes or returns another function is called a higher-order function. map, filter and setTimeout are all higher-order functions.
Returning functions#
The inner function "remembers" factor even after makeMultiplier has finished. That memory is called a closure — the subject of the next lesson.
Pure functions and side effects#
A pure function always returns the same output for the same input and changes nothing outside itself:
Pure functions are easier to test and reason about. Real programs need side effects (saving data, updating the page), but keep them at the edges and the core logic pure.
Parameters are passed by value — but objects are shared#
The function receives a copy of the reference, so changing properties affects the original object. Prefer returning a new object: return { ...user, name: "Changed" }.
Immediately-invoked and named function expressions#
You might see these in older code:
Modern code uses modules and block scope instead of IIFEs, but recursion (a function calling itself) remains useful for trees and nested data.
Good habits#
- Name functions with verbs:
calculateTotal,formatDate,fetchUser. - Keep each function small and focused on one job.
- Prefer returning values over changing outside variables.
- More than three parameters? Pass an object instead:
createUser({ name, email, role }).
Common mistakes#
- Forgetting
returnin a function with braces:(n) => { n * 2 }returnsundefined. - Calling instead of passing a callback:
setTimeout(sayHi(), 1000)runssayHiimmediately. PasssayHi. - Returning an object literal from an arrow without parentheses.
What's next#
Next: where variables are visible — scope — plus hoisting and the powerful idea of closures.
Check your understanding
Quick quiz
1.Which is a valid arrow function that doubles a number?
2.Given
function greet(name = "friend") { return "Hi " + name; }, what doesgreet()return?3.What does
const f = () => { value: 1 }; f()return?
Finished reading?
Mark this lesson complete to track your progress.