DEV Community

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

Posted on

1

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.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay