Thursday, 17 Jul 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
improve-logo improve-logo
  • Home
  • HTML

    What are the different types of HTML tags?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What are the async and defer attributes in the “script” tag?

    By Chief Editor

    What is the difference between “HTML” and “HTML5”?

    By Chief Editor
  • JavaScript

    What are the Rest and Spread operators in JavaScript?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is a Promise in JavaScript, and what are its parameters?

    By Chief Editor

    Difference between document.createElement and document.createElementFragement in JavaScript?

    By Chief Editor

    What are the map, filter, and reduce methods in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Frontend Interview

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor
  • Backend Interview

    Explain Call, Apply and Bind in JavaScript.

    By Chief Editor

    What is Flex Box in CSS?

    By Chief Editor

    What is localization in React?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    Difference between display none and visibility hidden in CSS?

    By Chief Editor
  • Nodejs
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
ImproveImprove
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • Pages
    • Home
    • Blog Index
    • Contact Us
    • Search Page
    • 404 Page
  • Features
    • Post Headers
    • Layout
  • Personalized
    • My Feed
    • My Saves
    • My Interests
    • History
  • About
  • Categories
    • Technology
  • Categories
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.

Home Explain Call, Apply and Bind in JavaScript.

JavaScript

Explain Call, Apply and Bind in JavaScript.

Chief Editor
Last updated: February 16, 2025 6:13 pm
Chief Editor
Share
SHARE

Call, Apply, and Bind in JavaScript

In JavaScript, call(), apply(), and bind() are methods that allow you to control the value of this when calling a function. They are especially useful when working with objects and borrowing methods.

Contents
1. call() Method2. apply() Method3. bind() MethodKey Differences Summary:When to Use Which?Practical Example Combining All:

1. call() Method

The call() method invokes a function immediately with a specific value for this, and you can pass arguments individually.

Syntax:

javascriptCopyEditfunctionName.call(thisArg, arg1, arg2, ...)

Example:

javascriptCopyEditconst person = {
    name: 'Alice',
    greet: function (city) {
        console.log(`Hello, my name is ${this.name} and I live in ${city}`);
    }
};

const person2 = { name: 'Bob' };

person.greet.call(person2, 'New York');
// Output: Hello, my name is Bob and I live in New York

Key Points:

  • call() calls the function immediately.
  • thisArg is the value of this inside the function.
  • Arguments are passed individually.

2. apply() Method

The apply() method is similar to call(), but it takes arguments as an array instead of individual values.

Syntax:

javascriptCopyEditfunctionName.apply(thisArg, [arg1, arg2, ...])

Example:

javascriptCopyEditconst person = {
    name: 'Alice',
    greet: function (city, country) {
        console.log(`Hello, my name is ${this.name} and I live in ${city}, ${country}`);
    }
};

const person2 = { name: 'Bob' };

person.greet.apply(person2, ['Los Angeles', 'USA']);
// Output: Hello, my name is Bob and I live in Los Angeles, USA

Key Points:

  • apply() calls the function immediately.
  • Arguments are passed as an array.
  • Useful when you don’t know the number of arguments in advance (e.g., Math.max.apply(null, [1, 2, 3])).

3. bind() Method

The bind() method returns a new function with a specific value for this, but does not call the function immediately.

Syntax:

javascriptCopyEditconst boundFunction = functionName.bind(thisArg, arg1, arg2, ...)

Example:

javascriptCopyEditconst person = {
    name: 'Alice',
    greet: function (city) {
        console.log(`Hello, my name is ${this.name} and I live in ${city}`);
    }
};

const person2 = { name: 'Bob' };

const boundGreet = person.greet.bind(person2, 'Chicago');
boundGreet(); // Output: Hello, my name is Bob and I live in Chicago

Key Points:

  • bind() does not call the function immediately.
  • Returns a new function with this permanently set to thisArg.
  • Useful when passing functions as callbacks or preserving this context in asynchronous operations.

Key Differences Summary:

MethodCalls the Function Immediately?Arguments Passed Individually or as an Array?Returns a New Function?
call()YesIndividuallyNo
apply()YesAs an arrayNo
bind()NoIndividuallyYes

When to Use Which?

Use CaseRecommended Method
Call a function immediately with a specific thiscall() or apply()
Pass arguments as an arrayapply()
Create a new function with this bound for later executionbind()
Event handlers, callbacks that need a fixed thisbind()

Practical Example Combining All:

javascriptCopyEditconst person = {
    name: 'Alice',
    age: 25
};

function introduce(city, country) {
    console.log(`Hi, I'm ${this.name}, ${this.age} years old, from ${city}, ${country}`);
}

// call
introduce.call(person, 'New York', 'USA');

// apply
introduce.apply(person, ['London', 'UK']);

// bind
const boundIntroduce = introduce.bind(person, 'Paris', 'France');
boundIntroduce();

Output:

rustCopyEditHi, I'm Alice, 25 years old, from New York, USA
Hi, I'm Alice, 25 years old, from London, UK
Hi, I'm Alice, 25 years old, from Paris, France
Share This Article
Email Copy Link Print
Previous Article Explain Deep Copy and Shallow Copy in JavaScript.
Next Article What is Flex Box in CSS?
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

What is props drilling in React?

Props Drilling is a situation in React where data (props) needs to be passed through…

By Chief Editor

What are the Rest and Spread operators in JavaScript?

Rest and Spread Operators in JavaScript (...) The Rest Operator and Spread Operator both use…

By Chief Editor

What is a Closure in JavaScript?

A closure in JavaScript is a function that remembers the variables from its outer lexical…

By Chief Editor

You Might Also Like

Frontend InterviewJavaScript

How to Reverse a String in JavaScript: Two Essential Methods

By Chief Editor
JavaScriptJavaScript Interview

What is memoization in JavaScript?

By Chief Editor
JavaScriptFrontend Interview

Explain Deep Copy and Shallow Copy in JavaScript.

By Chief Editor
JavaScriptJavaScript Interview

What is Arrow and Normal Function in JavaScript?

By Chief Editor
improve-logo

Code Reveals is a cutting-edge software development company dedicated to delivering high-quality, scalable, and innovative solutions for businesses of all sizes. Our team of expert developers, designers, and engineers specializes in creating custom software, web applications, mobile apps, and enterprise solutions that are tailored to meet the unique needs of our clients.

Most Famous
  • HTML
  • CSS
  • JavaScript
  • Node
Top Categories
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
Usefull Links
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?