What's the difference between .call and .apply?
Quiz · JavaScript
Medium
3 min
javascript
Both .call and .apply are used to invoke functions and the first parameter will be used as the value of this within the function. However, .call takes in comma-separated arguments as the next arguments while .apply takes in an array of arguments as the next argument. An easy way to remember this is C for call and comma-separated and A for apply and an array of arguments.
function add(a, b) {
return a + b;
}
console.log(add.call(null, 1, 2)); // 3
console.log(add.apply(null, [1, 2])); // 3
Adapted from the Front End Interview Handbook by Yangshun Tay and contributors (MIT License). Source
Spotted a mistake or a rights concern? Report an issue