🟢 EasyFunctions📖 Theory Question

What is the difference between call, apply, and bind?

💡

Hint

All set "this" — call=comma, apply=array, bind=returns new fn

Full Answer

All three explicitly set this:

  • call(thisArg, arg1, arg2) — invoke immediately, args individually
  • apply(thisArg, [args]) — invoke immediately, args as array
  • bind(thisArg, arg1) — returns new permanently-bound function
function greet(greeting, punct) {
  return `${greeting}, ${this.name}${punct}`;
}
const user = { name: 'Priya' };
greet.call(user, 'Hello', '!');     // "Hello, Priya!"
greet.apply(user, ['Hi', '.']);     // "Hi, Priya."
const fn = greet.bind(user, 'Hey');
fn('?');                            // "Hey, Priya?"
💡 Call=Comma, Apply=Array, Bind=returns Bound fn

More Functions Questions

🟢 EasyHow do arrow functions differ from regular functions?→🟢 EasyWhat is a pure function and why does it matter?→🟢 EasyWhat are Higher-Order Functions (HOF)?→🟢 EasyWhat is an IIFE (Immediately Invoked Function Expression) and when do you use it?→

Practice this in a timed sprint →

5 free questions, no signup required

âš¡ Start Sprint