> Articles > call(), apply() and bind() in JavaScript

call(), apply() and bind() in JavaScript

In this article, we will explain call(), apply() and bind() methods in JavaScript.

A function is an object and every function has methods call(), apply and bind() in JavaScript. These methods are used to set the value of this. They are very useful to use this appropriately and in many other scenarios like Function Borrowing, Function Currying. Let's see the call(), apply() and bind() methods in JavaScript in detail with examples.

call()

call() method in JavaScript is used to invoke the function, it will pass an object as first parameter which will be the value of this inside function.

Syntax

functionName.call(objectName);

functionName.call(objectName, param1, param2,...);

Implementation

call.js

call(), apply, bind() in JavaScript - call() method example

Output

call(), apply, bind() in JavaScript - call() method example output

apply()

apply() method in JavaScript is same as call(). It is used to invoke the function but the parameters will be passed in array. This may be very useful in scenarios where we get the input in form of array or we have to pass variable number of arguments.

Syntax

functionName.apply(objectName);

functionName.apply(objectName, [param1, param2,...]);

Implementation

apply.js

call(), apply, bind() in JavaScript - apply() method example

Output

call(), apply, bind() in JavaScript - apply() method example Output

bind()

bind() method in JavaScript is used to create new function, it will pass an object as parameter which will be the value of this inside function.

Syntax

var bFunction = functionName.bind(objectName);

Implementation

bind.js

call(), apply, bind() in JavaScript - bind() method example

Output

call(), apply, bind() in JavaScript - bind() method example Output


Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books C In Depth, Data Structures Through C In Depth. He has 18+ years experience in industry and worked on architecture and design of multiple products. This article call(), apply() and bind() in JavaScript is from his Advanced JavaScript course.