Skip to main content

call vs apply

In short:

MethodHow arguments are passedCall example
calllisted comma-separatedfunc.call(thisArg, arg1, arg2, arg3)
applypassed as an arrayfunc.apply(thisArg, [arg1, arg2, arg3])

Example for clarity

javascript
function greet(city, job) { console.log(`I'm ${this.name} from ${city}, working as ${job}`); } const user = { name: 'Bohdan' }; // call - arguments comma-separated greet.call(user, 'Kyiv', 'Frontend developer'); // apply - arguments as an array greet.apply(user, ['Kyiv', 'Frontend developer']);

Both calls output:

javascript
I'm Bohdan from Kyiv, working as Frontend developer

When to use which

ScenarioBetter to use
Arguments are known in advancecall
Arguments are already in an arrayapply

Example: array of arguments

javascript
function sum(a, b, c) { return a + b + c; } const nums = [1, 2, 3]; // Without apply - you'd have to write it manually console.log(sum.call(null, nums[0], nums[1], nums[2])); // With apply - just pass the array console.log(sum.apply(null, nums)); // 6

Modern equivalent with Spread

In modern JS (ES6+) apply is often replaced with the spread operator:

javascript
const nums = [1, 2, 3]; console.log(sum(...nums)); // the same result as sum.apply(null, nums)

Summary

Propertycallapply
Context (this)Explicitly given as the first argumentSame
Passing argumentsComma-separatedAs an array
ReturnsThe function's resultThe function's result
Calls immediatelyyesyes
Modern alternativeSpread: func(...args)Spread: func(...args)

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.