JS Functions Are Objects: What That Actually Means
In JavaScript, functions are objects. Not a loose analogy — they can have properties, be stored, passed, and returned. What distinguishes them is that they can be called.
In JavaScript, functions are objects. This is not a loose analogy. According to the MDN Web Docs, functions in JavaScript are first-class objects because they can be passed to other functions, returned from functions, and assigned to variables and properties. They can also have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called.
Understanding this changes how you read and reason about JavaScript code.
A Function Has Properties
Because a function is an object, it carries built-in properties.
name returns the name of the function as a string.
length returns the number of parameters the function declares.
function greet(name, greeting) {
return greeting + ', ' + name;
}
console.log(greet.name); // 'greet'
console.log(greet.length); // 2
You can also attach custom properties directly to a function, in the same way you would to any object.
function counter() {
counter.calls++;
}
counter.calls = 0;
counter();
counter();
console.log(counter.calls); // 2
A Function Can Be Stored, Passed, and Returned
Storing a function in a variable:
const add = function(a, b) {
return a + b;
};
Passing a function as an argument:
function run(fn) {
return fn(2, 3);
}
console.log(run(add)); // 5
Returning a function from a function:
function multiplier(factor) {
return function(n) {
return n * factor;
};
}
const triple = multiplier(3);
console.log(triple(5)); // 15
A Function Has a Prototype Property
Every function in JavaScript has a prototype property. This is an object that becomes the prototype of any object created when the function is used as a constructor with the new keyword.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return 'Hello, ' + this.name;
};
const alice = new Person('Alice');
console.log(alice.greet()); // 'Hello, Alice'
greet is not defined on alice directly. It is found on Person.prototype through the prototype chain.
Functions Inherit call, apply and bind
Because functions are objects, they inherit methods from Function.prototype. The three most important are call, apply and bind, which allow you to control what this refers to when a function runs.
call invokes the function immediately, with this set to the first argument and additional arguments passed individually.
function greet() {
return 'Hello, ' + this.name;
}
const user = { name: 'Alice' };
console.log(greet.call(user)); // 'Hello, Alice'
apply works identically to call but accepts arguments as an array.
function sum(a, b) {
return a + b;
}
console.log(sum.apply(null, [3, 4])); // 7
bind returns a new function with this permanently fixed to the provided value. It does not invoke the function immediately.
const greetAlice = greet.bind(user);
console.log(greetAlice()); // 'Hello, Alice'
What to Do Now
Inspect a function directly in your browser console:
function example(a, b, c) {}
console.log(example.name); // 'example'
console.log(example.length); // 3
console.log(typeof example); // 'function'
console.log(example instanceof Object); // true
The last line confirms it. A function is an instance of Object. It is not a special primitive type separate from the object system. It is an object with the added ability to be invoked.