W3docs

How to Convert Arguments Object to an Array

Read this JavaScript tutorial and learn information about the useful methods that are used for converting array-like arguments object to an Array easily.

The arguments object is an Array-like object which represents the arguments passed in when executing a function. To convert it to Array, there are several methods that we are going to discuss in the scope of this article.

Array.from

There is a method in ECMAScript 2015 that converts the arguments into an array. You can use the <kbd class="highlighted">Array.from()</kbd> or the spread operator like this:

JavaScript converts the arguments object into an array using the Array.from method

javascript— editable

The <kbd class="highlighted">Array.from()</kbd> method is used to create a new, shallow-copied Array instance from an array-like or iterable object.

Spread Syntax

If you use ES6, the spread syntax allows you to convert the arguments object into an Array:

JavaScript converts the arguments object into an array using spread syntax

javascript— editable

The spread syntax expands the arguments object into a new Array, giving you access to all standard Array methods.

slice()

The slice() method normally works on Arrays. However, it can be borrowed to convert any array-like object (like arguments) into a real Array.

The call() and apply() methods allow you to manually set the this value inside a function. By using call(), you can pass the arguments object as the this context to slice(), making it treat the object as an Array.

Info

The Array-like object means that arguments has a length property and properties indexed from zero, but it doesn't have the built-in methods of Array, such as <kbd class="highlighted">forEach()</kbd> and <kbd class="highlighted">map()</kbd>.

Let’s take a look at this example:

JavaScript converts the arguments object into an array using slice()

javascript— editable

This approach works because slice() only requires an object with a length property and numeric indices, which the arguments object provides.