DEV Community

Amitav Mishra
Amitav Mishra

Posted on • Originally published at jscurious.com

1

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

In JavaScript this refers to the owner object. If you want to attach some extra properties to a function, then you can do that with these 3 methods and can access that extra property using the this keyword. Let’s learn in deep about these 3 methods.

The call() method

The call() method calls a function with a given value and other arguments provided in the function.

const address = {city: 'Sonepur', state: 'Odisha'};

function print(name) {
    console.log('Name: ' + name + ', Address: ' + this.city + ', ' + this.state);
}

print.call(address, 'Amitav');
// Name: Amitav, Address: Sonepur, Odisha
Enter fullscreen mode Exit fullscreen mode

Here in the example above, call() attaches the address object to the print() function and print() function can access this address object using this.
You can provide any type of value for this.

function print() {
    console.log('Hello ' + this);
}

print.call('World'); // Hello World

print.call(245); // Hello 245
Enter fullscreen mode Exit fullscreen mode
function print() {
    console.log(this[0] + ' ' + this[1]);
}

print.call(['Hello', 'World']); // Hello World
Enter fullscreen mode Exit fullscreen mode

The apply() method

This method invokes the function and allows you to pass the arguments as an array.

const address = {city: 'Sonepur', state: 'Odisha'};

function print(name, age) {
    console.log(name +', Age: ' + age + ', Address: ' + this.city + ', ' + this.state);
}

print.apply(address, ['Amitav', 24]);
//Amitav, Age: 24, Address: Sonepur, Odisha
Enter fullscreen mode Exit fullscreen mode

Both call() and apply() works the same way. The only difference is that call() expects all parameters to be provided one by one, whereas apply() allows you to pass in arguments as an array.

The bind() method

This method returns a new function with the value bound to it, which you can use to call the function with the required arguments.

const address = {city: 'Sonepur', state: 'Odisha'};

function print(name, age) {
    console.log(name +', Age: ' + age + ', Address: ' + this.city + ', ' + this.state);
}

const bindAddress = print.bind(address);

bindAddress('Amitav', 24);
//Amitav, Age: 24, Address: Sonepur, Odisha
Enter fullscreen mode Exit fullscreen mode

You may also like

Thanks for your time
Find more Web dev blogs on jscurious.com

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay