DEV Community

Cover image for call() , apply() and bind() Method In JavaScript
Md Pervez Hossain
Md Pervez Hossain

Posted on

call() , apply() and bind() Method In JavaScript

What is the call() Method in js ?

call() method is a built-in javascript method that allows us to invoke a function with specified โ€œthisโ€ context.

Key Features ๐Ÿ‘

  • First parameter Sets โ€œthisโ€ context value for the function.
  • Make a function Reusable.
  • Apply the same function to different objects.
  • Others parameter passed with comma seperated
  • It calls the function directly

Image description

What is the apply() Method in js ?

apply() method is a built-in javascript method that allows us to invoke a function with specified โ€œthisโ€ context.

Key Features ๐Ÿ‘

  • First parameter Sets โ€œthisโ€ context value for the function.
  • Make a function Reusable.
  • Apply the same function to different objects.
  • Other parameters are passed as an array:
  • It calls the function Immediately

Image description

What is the bind() Method in js ?

The bind() method is a built-in JavaScript method that creates a copy of the original function and returns a new function with a specified this context, rather than invoking the original function immediately.

Key Features ๐Ÿ‘

  • First parameter Sets โ€œthisโ€ context value for the function.
  • Make a function Reusable.
  • Apply the same function to different objects.
  • Others parameter passed with comma seperated
  • creates a copy of the original function and returns a new function
  • Need to call the returned function to access the original functionality

Image description

Key Differences between call() , apply() and bind() Methods :

Execution Timing:

  • call() and apply() invoke the function immediately.
  • bind() returns a new function without invoking it, allowing deferred execution.

Arguments Handling:

  • call() accepts arguments as comma-separated values.
  • apply() requires arguments as an array.
  • bind() also uses comma-separated arguments, but these are pre-set for later when the function is invoked.

Return Value:

  • call() and apply() return the result of the function call.
  • bind() returns a new function with the specified this context.

Write down in the comment box your knowledge about the call(), apply(), and bind() methods.

Top comments (0)