What is the difference between call and apply?

What is the difference between call and apply?

Add Comment


  • 2 Answer(s)

    The difference between call and apply:

    • apply will invoke the function with arguments as an array.
    • Call requires the parameters be listed explicitly.
    • call() method takes the arguments separated by comma
    • apply() method takes the array of arguments.
    Answered on May 4, 2019.
    Add Comment

    The difference is that apply lets you invoke the function with arguments as an array; callrequires the parameters be listed explicitly. A useful mnemonic is A for array and C for comma.”

    See MDN’s documentation on apply and call.

    Pseudo syntax:

    theFunction.apply(valueForThis, arrayOfArgs)

    theFunction.call(valueForThis, arg1, arg2, ...)

    There is also, as of ES6, the possibility to spread the array for use with the call function, you can see the compatibilities here.

    Sample code:

    function theFunction(name, profession) {
        console.log("My name is " + name + " and I am a " + profession +".");
    }
    theFunction("John", "fireman");
    theFunction.apply(undefined, ["Susan", "school teacher"]);
    theFunction.call(undefined, "Claude", "mathematician");
    theFunction.call(undefined, ...["Matthew", "physicist"]); // used with the spread operato

     

    Answered on June 10, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.