Arguments are passed

Tell us what’s happening:
Who can explain the following question to me?

  1. Why arr = [1, 2, 3, 1, 2, 3] while I pass [1, 2, 3, 1, 2, 3], 2, 3 into function destroyer(arr)? I think it will be arr = [[1, 2, 3, 1, 2, 3], 2, 3].
  2. What is the difference between Array.prototype.slice.call(arguments) and Array.from(arguments)?
    Thank you so much!!!
    Your code so far

function destroyer(arr) {
  console.log(arr);
  console.log(Array.prototype.slice.call(arguments));
  console.log(Array.from(arguments));
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/80.0.180 Chrome/74.0.3729.180 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy

Hey @paukerspinner,

  1. from the point of readability construct like destroyer(source, target) where both source and target are arrays would be better choice in my opinion. Nevertheless if the goal is to learn how to access arguments then destroyer(arr) is absolutely fine to me!

  2. If .slice() doesn’t have any arguments then it acts like Array.from() by creating a shallow copy, but they are clearly not the same because .slice() supposed to receive arguments and to be used for slicing, as the name suggests. So if you want to convert arguments to array, use Array.from() or simply spread it (which is the same): [...arguments]. In this particular example however you don’t need first element of arguments as you already defined it as arr, so:

Array.prototype.slice.call(arguments, 1) // NOTE the second argument

…is the way to go :wink:

1 Like

Thank you!!! :wink: :wink: :wink: