Mastering JavaScript’s call, apply, and bind – The Easy Way!

Share

Have you ever been confused about call, apply, and bind in JavaScript? Don’t worry, you’re not alone! These three methods are powerful, but they can be tricky to understand at first. In this article, we’ll break them down in the simplest way possible with examples that make sense.

What Are call, apply, and bind?

All three of these functions are used to control this keyword in JavaScript. When working with objects and functions, sometimes you need to tell JavaScript what this should refer to explicitly. That’s where these methods come in!

Let’s take a look at each one.

1. call – Execute a Function Immediately

The call method is used to invoke a function immediately and explicitly set the value of this.

Syntax:

functionName.call(thisArg, arg1, arg2, …)

Example:

const person = {
name: “Alice”
};

function greet(age) {
console.log(`Hello, my name is ${this.name} and I am ${age} years old.`);
}

greet.call(person, 25); // Hello, my name is Alice and I am 25 years old.

Here, call sets this to person, so the function correctly accesses person.name.

2. apply – Similar to call, But Uses an Array

The apply method is almost the same as call, but instead of passing arguments one by one, you pass them as an array.

Syntax:

functionName.apply(thisArg, [arg1, arg2, …])

Example:

greet.apply(person, [25]); // Hello, my name is Alice and I am 25 years old.

This is useful when you already have an array of arguments and don’t want to manually pass each one.

3. bind – Returns a New Function

The bind method doesn’t execute the function immediately. Instead, it returns a new function with this permanently set to the specified value.

Syntax:

const newFunction = functionName.bind(thisArg, arg1, arg2, …)

Example:

const boundGreet = greet.bind(person, 25);
boundGreet(); // Hello, my name is Alice and I am 25 years old.

Now boundGreet is a function that will always use person as this, no matter how or when it’s called.

Differences at a Glance

Differences at a Glance

When Should You Use Each?

  • Use call when you want to invoke a function immediately and pass arguments one by one.
  • Use apply when you want to invoke a function immediately but have arguments in an array.
  • Use bind to create a new function with a specific value.

Final Thoughts

Understanding call, apply, and bind will make you a better JavaScript developer. They are essential for handling this in a controlled way and can help you write cleaner, more flexible code.

At LN Webworks, we specialize in creating high-performance JavaScript solutions that offer seamless user experiences. Whether it’s custom web development, advanced UI/UX, or scalable applications, our expert team ensures that your projects are built with the best coding practices.

Ready to take your JavaScript skills—or your web development projects—to the next level? Contact LN Webworks today!

Sunil
The Author
Sunil

Web Developer

Read More Blogs

Illustration comparing a custom CRM and an off-the-shelf CRM, showing a business professional choosing between a tailored CRM solution and a packaged CRM software. Other, SaaS Development

SaaS CRMs win on speed. Custom wins on fit and ownership. This isn’t a pitch for either one. Every early-stage SaaS company hits this decision within the first year, usually right after the founder realizes a spreadsheet isn’t a CRM.  The default answer is almost always a SaaS CRM: HubSpot, Salesforce, Zoho, sign up in […]

Illustration of a modernization readiness assessment highlighting business alignment, technology assessment, process optimization, data integration, people and skills, risk and compliance, and a roadmap for successful digital transformation Other

Most “audits” and “assessments” promise clarity but deliver a sales deck with language that lacks clarity. You book the call expecting evidence about your specific platform, and what shows up is a generic pitch for whichever package is easiest for the firm to sell.  That’s the actual reason process transparency matters here, not because a […]

LN Webworks illustration of the BAT-BMS Bluetooth exploit, app ban, and access-control review process Other

In late June and early July, videos started circulating on Instagram of people walking up to moving e-rickshaws, tapping a phone, and watching the vehicle lose power mid-ride.  It spread as a prank trend before anyone fully understood the mechanism. Drivers in Patna (India) reported it first, assuming mechanical failure, until investigators traced it to […]

LN Webworks Logo
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.