The slice method

Please consider the line of code below:

var args = Array.prototype.slice.call(arguments);

isn’t args = Array.prototype.slice.call(arguments) like doing : args = arguments.slice(), if not, why and what is the difference please explain if possible thanks <3

Have you tried doing this? What happened?

args = arguments.slice()
TypeError: arguments.slice is not a function

Why do you think slice is not a function?

It was the console message not me :rofl:
the console returns that message

Yeah I know and it’s true - it’s not a function :smiley:

I’m asking you, why it’s not a function?

You can always check. If it is not a function, then what it is?

console.log(arguments.slice)

i don’t know, can you tell me?

So, what did you get when you console.log arguments.slice?

Do you know the arguments object?

What do you mean do I know it?

I think you answered the question yourself with this link:

arguments is an Array -like object

You cannot use slice method, because there is no slice method. If you would console.log(arguments.slice) you would get undefined because slice is specific to arrays which arguments is not.

That’s why we have to use Array.prototype.

but why does it work with Array.prototype ?

MDN:

The arguments object is not an Array. It is similar, but lacks all Array properties except length. For example, it does not have the pop() method.

However, it can be converted to a real Array :

var args = Array.prototype.slice.call(arguments);
1 Like

A somewhat more readable version would be

var args = Array.from(arguments).slice()

arguments is an Array-like object, which means that

  • the JavaScript Array methods can’t be used on it, but
  • it can be converted to an Array with Array.from()
1 Like