javascriptarraysargumentsrest-parameters

What is the difference between Array.prototype.slice.call(arguments) vs [...args]?


I know that Array.prototype.slice.call(arguments) is used to create arguments into a real array as shown below:

function foo() {
  // convert arguments to an array:
  var argsArray = Array.prototype.slice.call(arguments);
  // . . . foo's logic . . .
}

and I can also use the spread operator to achieve the same functionality as below

function foo(...argsArray) {
  // . . . foo's logic . . .
} 

Apart from less verbose in using spread operator, what are the differences between both approaches?


Solution

  • In your example, both variants should work the same.

    The arguments object was mainly used before rest parameters and the spread operator was a thing. According to MDN, it shouldn't be used in modern JavaScript anymore: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments