Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

What is the difference between call and apply in JavaScript?

Do you know what will be the output of this code?

JavaScript
function sum(a, b) {
  return this.base + a + b;
}

console.log(sum.apply({ base: 1 }, [2, 3]));
Show answer and explanation

The answer is 6.

apply calls sum with this set to { base: 1 } and spreads the array into arguments: 1 + 2 + 3. The call equivalent is sum.call({ base: 1 }, 2, 3).

Take the full quiz Next question →

More this questions

  1. Why does this become undefined when you extract a method?
  2. Why does an arrow function method return undefined for this?
  3. Can you change this on a bound function with call()?
  4. Why does an arrow function in setTimeout keep this?
  5. What happens when a constructor returns an object?

Related topics