Skip to content

JavaScript function apply vs call | Difference

  • by

The difference between call() and apply() is that call() accepts an argument list, while apply() accepts a single array of arguments.

Pseudo syntax:

theFunction.apply(valueForThis, arrayOfArgs)
// or
.apply(this, [...])
theFunction.call(valueForThis, arg1, arg2, ...)
// or
.call(this, param1, param2, param3, param4...)

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

JavaScript function apply vs call

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

<!DOCTYPE html>
<html>
<body>
  <script>
    function func1(name, profession) {
      console.log(name + " is a " + profession +".");
    }
    
    func1("John", "fireman");
    func1.apply(undefined, ["Susan", "school teacher"]);
    func1.call(undefined, "Claude", "mathematician");
    func1.call(undefined, ...["Matthew", "physicist"]); // spread operator

</script>
</body>
</html>

Output:

JavaScript function apply vs call Difference

Source: stackoverflow.com

Do comment if you have any doubts or suggestions on this Js function difference topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading