Functions as objects
In JavaScript, functions are objects. That means a function can be stored in a variable, passed to another function, returned from a function, and even given its own properties, just like any other object. This idea, often described as functions being "first-class values", is behind callbacks, event handlers, array methods like map and most modern JavaScript patterns.
Functions are values you can move around
// 1. Stored in a variable
const greet = function (name) {
return `Hello, ${name}`;
};
// 2. Stored in an object or array
const formatters = {
upper: (s) => s.toUpperCase(),
lower: (s) => s.toLowerCase(),
};
const steps = [formatters.upper, (s) => s + '!'];
// 3. Passed as an argument
function applyAll(value, fns) {
return fns.reduce((result, fn) => fn(result), value);
}
console.log(applyAll('hi', steps)); // "HI!"
// 4. Returned from another function
function makeMultiplier(factor) {
return (n) => n * factor;
}
const triple = makeMultiplier(3);
console.log(triple(5)); // 15
// 5. Given properties of their own
function countCalls() {
countCalls.calls += 1;
}
countCalls.calls = 0;
countCalls();
countCalls();
console.log(countCalls.calls); // 2
Notice the difference between greet and greet('Asha'). The name alone refers to the function object. Adding parentheses calls it and gives you its return value.
Built-in properties and methods every function has
| Member | What it gives you |
|---|---|
fn.name | The function's name, useful in logs and stack traces. |
fn.length | How many parameters it declares, not counting ones with defaults or rest parameters. |
fn.call(thisArg, a, b) | Calls it with a chosen this and listed arguments. |
fn.apply(thisArg, [a, b]) | Same, but arguments come as an array. |
fn.bind(thisArg) | Returns a new function with this fixed permanently. |
fn.prototype | The object used as the prototype when a regular function is called with new. |
The fact that typeof greet returns "function" rather than "object" is a convenience. Under the hood, functions are special callable objects that inherit from Function.prototype, which is where call, apply and bind live.
Higher-order functions
A function that takes or returns another function is called higher-order. You use them constantly: array.map(fn), array.filter(fn), setTimeout(fn, 1000) and button.addEventListener('click', fn) all accept a function and decide when to call it. makeMultiplier above is a function factory: it returns a new function that remembers factor through a closure.
A common mix-up: calling instead of passing
The most frequent beginner error is adding parentheses when a function should be passed, not run. button.addEventListener('click', save()) calls save immediately while the page loads and passes its return value, usually undefined, as the listener. Clicks then do nothing. Write addEventListener('click', save), or wrap it if you need arguments: addEventListener('click', () => save(draftId)).
A related trap is passing an object's method on its own, like setTimeout(user.sayHi, 100). The function travels without its object, so this inside it is no longer user. Use user.sayHi.bind(user) or an arrow wrapper.
Why this is worth understanding
Treating functions as values lets you write small, reusable pieces and combine them, rather than repeating logic. Sorting with a custom comparison, validating with a list of rule functions, or configuring behaviour by passing a function are all applications of this one idea. The next lesson looks at the Function constructor, another sign that functions really are objects.
Key ideas
Functions can be assigned, stored, passed, returned and given properties. Every function carries name, length, call, apply and bind. Pass a function by its name without parentheses, and bind methods when their this matters.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.