WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

JavaScript · Intermediate · question 22 of 100

Can you explain the difference between "call," "apply," and "bind" methods in JavaScript?

📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

In JavaScript, call(), apply(), and bind() are methods that allow you to call a function with a specific this value, as well as passing arguments to the function. Although they are used for similar purposes, they have some differences that are important to understand.

call(): The call() method is used to call a function with a specific this value and arguments provided individually. Here’s an example:

    function greeting() {
        console.log("Hello, " + this.name);
    }
    
    var person1 = { name: "John" };
    var person2 = { name: "Jane" };
    
    greeting.call(person1); // Outputs "Hello, John"
    greeting.call(person2); // Outputs "Hello, Jane"

In this example, the call() method is used to call the greeting function with the this value set to person1 and person2 objects.

apply(): The apply() method is similar to call(), but it accepts arguments in the form of an array. Here’s an example:

    function sum(a, b, c) {
        return a + b + c;
    }
    
    var numbers = [1, 2, 3];
    
    console.log(sum.apply(null, numbers)); // Outputs 6

In this example, the apply() method is used to call the sum function with the this value set to null and arguments provided as an array.

bind(): The bind() method is used to create a new function with a specific this value and arguments, without calling the original function. Here’s an example:

    var person = {
        name: "John",
        sayHello: function() {
            console.log("Hello, " + this.name);
        }
    };
    
    var greet = person.sayHello.bind(person);
    
    greet(); // Outputs "Hello, John"

In this example, the bind() method is used to create a new function greet with the this value set to the person object.

In summary, call(), apply(), and bind() are methods in JavaScript that allow you to call a function with a specific this value and arguments. call() and apply() are similar, but apply() accepts arguments in the form of an array. bind() is used to create a new function with a specific this value and arguments, without calling the original function. Understanding how to use these methods can be helpful when working with objects and functions in JavaScript.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic JavaScript interview — then scores it.
📞 Practice JavaScript — free 15 min
📕 Buy this interview preparation book: 100 JavaScript questions & answers — PDF + EPUB for $5

All 100 JavaScript questions · All topics