DEV Community

Cover image for How-to: call() , apply() and bind() in JavaScript
Jamal
Jamal

Posted on

How-to: call() , apply() and bind() in JavaScript

This post covers some basic concepts in JavaScript. Every JavaScript programmer must know the concept and clear

Call()

The call() method calls a function with a given this value and arguments provided individually.

Syntax
func.call([thisArg[, arg1, arg2, ...argN]])

Alt Text

apply()

Similarly to call() method the first parameter in apply() method sets the "this" value which is the object upon which the function is invoked. In this case, it's the "obj" object above. The only difference of apply() with the call() method is that the second parameter of the apply() method accepts the arguments to the actual function as an array.

Alt Text

bind()

Syntax
let boundFunc = func.bind(context);

In JavaScript, function binding is happens using Bind() method. by this method, we can bind an object to a common function, so that the function gives different result when its need.
Alt Text

That's all for now. Thank you for reading and I hope this post will be helpful for beginners who are facing issues regarding the apply(), call(), and bind() methods of JavaScript.

Top comments (0)

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay