Question
JavaScript call() vs apply(): Difference, Use Cases, and Examples
Question
In JavaScript, what is the difference between using Function.prototype.apply() and Function.prototype.call() to invoke a function?
const func = function() {
alert("Hello world!");
};
func.apply();
func.call();
Are there any performance differences between these two methods? When is it better to use call() instead of apply(), and when should apply() be preferred?
Short Answer
By the end of this page, you will understand how call() and apply() invoke a function, how they set this, how they pass arguments differently, and when each one is useful in JavaScript code.
Concept
call() and apply() are both methods available on JavaScript functions. They let you invoke a function immediately while also controlling what this should refer to inside that function.
The core difference is how arguments are passed:
call(thisArg, arg1, arg2, arg3)passes arguments one by one.apply(thisArg, [arg1, arg2, arg3])passes arguments as an array or array-like object.
Why this matters
In JavaScript, this depends on how a function is called. Sometimes you want to borrow a function from one object and use it with another object, or call a function when your arguments already exist in an array. That is where call() and apply() are useful.
Important idea
If a function takes no arguments, then these two are effectively the same in everyday use:
func.call();
func.apply();
Both invoke the function immediately.
Performance note
In modern JavaScript, performance differences between call() and apply() are usually negligible for normal code. You should choose based on , not micro-optimizations.
Mental Model
Think of a function as a worker who needs two things before starting:
- Who they are working for → this is
this - What materials they are given → these are the arguments
call() says:
"Work for this person, and here are your materials one by one."
apply() says:
"Work for this person, and here is a single box containing all your materials."
So the job is the same. The only real difference is how the materials are handed over.
call()= separate argumentsapply()= one array of arguments
Syntax and Examples
Basic syntax
fn.call(thisArg, arg1, arg2, arg3);
fn.apply(thisArg, [arg1, arg2, arg3]);
thisArgis the value thatthisshould refer to inside the function.- The remaining values are the function arguments.
Example 1: No arguments
function greet() {
console.log("Hello world!");
}
greet.call();
greet.apply();
Both lines do the same thing here because:
- no custom
thisis needed - no arguments are being passed
Example 2: Setting this
function introduce() {
console.log(`Hi, I am ${this.name}`);
}
const person = { : };
introduce.(person);
introduce.(person);
Step by Step Execution
Consider this example:
function showMessage(greeting, punctuation) {
console.log(greeting + ", " + this.name + punctuation);
}
const user = { name: "Lina" };
showMessage.call(user, "Hello", "!");
showMessage.apply(user, ["Hello", "!"]);
What happens with call()
showMessage.call(user, "Hello", "!");
Step by step:
- JavaScript looks at the function
showMessage. call()invokes it immediately.thisinsideshowMessagebecomesuser.- The first argument becomes
greeting = "Hello".
Real World Use Cases
1. Reusing a method with another object
const person1 = {
name: "Sam",
sayName() {
console.log(this.name);
}
};
const person2 = { name: "Rita" };
person1.sayName.call(person2); // Rita
This is called function borrowing.
2. Calling a function with array data
function multiply(a, b, c) {
return a * b * c;
}
const values = [2, 3, 4];
multiply.apply(null, values); // 24
Useful when arguments come from:
- API responses
- form data
- parsed CSV rows
- dynamic configuration arrays
3. Converting array-like data in older patterns
Historically, apply() was often used with array-like objects such as .
Real Codebase Usage
In real projects, call() and apply() appear less often than basic function calls, but they still show up in useful patterns.
1. Method borrowing
Developers sometimes reuse built-in methods on array-like values.
const arrayLike = { 0: "a", 1: "b", length: 2 };
const result = Array.prototype.join.call(arrayLike, "-");
console.log(result); // a-b
2. Delegating arguments
A wrapper function may forward all received arguments.
function wrapper() {
return originalFunction.apply(this, arguments);
}
In modern JavaScript this is usually written as:
function () {
originalFunction.(, ...args);
}
Common Mistakes
1. Forgetting that call() and apply() invoke immediately
Some beginners think these methods just prepare a function. They do not. They run it right away.
function sayHi() {
console.log("Hi");
}
sayHi.call(); // runs now
sayHi.apply(); // runs now
2. Passing array arguments to call() by mistake
Broken code:
function add(a, b) {
return a + b;
}
console.log(add.call(null, [2, 3])); // wrong
Why it fails:
abecomes[2, 3]bbecomesundefined
Comparisons
| Feature | call() | apply() |
|---|---|---|
| Invokes function immediately | Yes | Yes |
Sets this | Yes | Yes |
| Argument format | Separate arguments | Array or array-like object |
| Good when arguments are already in an array | No | Yes |
| Good when arguments are known individually | Yes | Less convenient |
| Common modern replacement | fn.call(obj, ...args) | fn(...args) or fn.call(obj, ...args) |
vs normal function call
Cheat Sheet
Quick rules
call()andapply()both invoke a function immediately.- Both let you choose the value of
this. - Main difference: how arguments are passed.
Syntax
fn.call(thisArg, arg1, arg2, arg3);
fn.apply(thisArg, [arg1, arg2, arg3]);
Use call() when
- you know the arguments individually
- you want a straightforward function invocation with a custom
this
fn.call(obj, 1, 2, 3);
Use apply() when
- your arguments already exist in an array
- you are working with array-like objects in older code
fn.apply(obj, [1, 2, 3]);
Modern alternatives
FAQ
What is the main difference between call() and apply() in JavaScript?
The main difference is argument passing:
call()passes arguments one by oneapply()passes them as an array or array-like object
Do call() and apply() both change this?
Yes. Both let you set the this value for the function call.
Is call() faster than apply()?
In modern JavaScript engines, any performance difference is usually too small to matter in normal application code.
When should I use apply()?
Use apply() when your arguments are already in an array or array-like object.
When should I use call()?
Use call() when you want to pass arguments individually and set this explicitly.
Mini Project
Description
Build a small utility that formats user messages by calling the same function with different this values and argument styles. This demonstrates how call() and apply() work in practical code.
Goal
Create a reusable message formatter that can be invoked with different user objects using both call() and apply().
Requirements
- Create a function that uses
this.nameinside its output. - Invoke the function once with
call()and once withapply(). - Pass the message parts as separate arguments in one case and as an array in the other.
- Print the results to the console.
Keep learning
Related questions
Deep Cloning Objects in JavaScript: Methods, Trade-offs, and Best Practices
Learn how to deep clone objects in JavaScript, compare structuredClone, JSON methods, and recursive approaches with examples.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers with clear examples.
How JavaScript Closures Work: A Beginner-Friendly Guide
Learn how JavaScript closures work with simple explanations, examples, common mistakes, and practical use cases for real code.