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

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

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

In javascript a function is an object and every function has methods call(), apply and bind(). 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 JavaScript call(), apply() and bind() methods 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

callDemo.js

JavaScript call() method - Source Code Example

Output

call() method in JavaScript - 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

applyDemo.js

JavaScript apply() method - Source Code Example

Output

apply() method JavaScript - 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

bindDemo.js

JavaScript bind() method - Source Code Example

Output

bind() method JavaScript - 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 is from his course on Advanced JavaScript.