Difference between call and apply in JS

Learn about key difference between call and apply in JavaScript
31 October 2017   3268

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."

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

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 operator

// Output: 

// My name is John and I am a fireman.
// My name is Susan and I am a school teacher.
// My name is Claude and I am a mathematician.
// My name is Matthew and I am a physicist.

Frontend News Digest 9-15.11

Chrome will mark "fast" websites, PM2 4.2 released, Chrome Dev Summit 2019 video keynote and other interesting things
15 November 2019   182

Greetings! I hope your week went great! Here's new Python news digest.

Learn when to use SVG vs when to use canvas, how to use ffmpeg to combine many videos in one, check the results of front-end tooling survey and more.

Guides

  • When to Use SVG vs. When to Use Canvas

Learn where to use what

  • How to Use ffmpeg to Combine Multiple Videos to One 

Advice on how to combine multiple separate videos into a single one with ffmpeg.

Articles

  • Moving towards a faster web

One of the most popular web brousers is going to mark "fast" websites

  • The Front-End Tooling Survey 2019 - Results

3,000 developers took part in the survey, time to check a detailed report

  • Semantic Sidenotes for The Web

Improvement proposal for sidenotes implementation in HTML

  • A Super Weird CSS Bug That Affects Text Selection

Did you know that using a selector without any style properties can cause no styling to be applied at all? 

Updates

  • linkinator

Site crawler and broken link checker

  • PM2 4.2

The newest update for the a production process manager for Node.js applications with a built-in load balance brings a lot of new features

Video

  • Keynote (Chrome Dev Summit 2019)

  • HTML isn’t done!