How to convert arguments object to an array in JavaScript

The arguments object is an array-like object accessible inside all non-arrow functions that represents the values of the arguments passed to that function. You can use numerical indexes to access the values of arguments from the arguments object. For example, the first argument can be accessed as arguments[0], the second argument can be accessed as arguments[1], and so on.

The arguments object is a lot like an array, but it is not a real array. Although it has the length property, you can not use the common array methods like map(), slice(), and filter() on it.

There are multiple ways to convert an arguments object to a true Array object.

Rest Parameters

The rest parameter syntax was introduced in ES6 to represent an unspecified number of arguments as an array. To use rest parameters, you can prefix the function's last parameter with ... (spread operator). It will convert all remaining user-supplied arguments into a standard JavaScript array.

Since the rest parameter is a real Array instance, unlike the arguments object, you do not need to perform any conversion:

const sort = (...numbers) => {
  return numbers.sort((a, b) => a - b)
}

sort(1, 4, 5, 2)
// [ 1, 2, 4, 5 ]

Array.from() Method

Another way to convert the arguments object to an array in ES6 is by using the Array.from() method. This method converts an array-like or iterable object into an Array instance:

function sort() {
  return Array.from(arguments).sort((a, b) => a - b)
}

sort(1, 4, 5, 2)
// [ 1, 2, 4, 5 ]

Array.prototype.slice() Method

Finally, the last method to convert an arguments object to an array is the Array.prototype.slice() method. Much like converting a NodeList to an array, the Array.slice() method takes in the arguments object and transforms it into an actual array:

function sort() {
  const args = Array.prototype.slice.call(arguments)
  return args.sort((a, b) => a - b)
}

const sorted = sort(1, 4, 5, 2)

console.log(sorted)
// [ 1, 2, 4, 5 ]

You can also use a concise form of the slice() method:

const args = [].slice.call(arguments)

The Array.prototype.slice.call() works in all modern and old browsers, including IE 6+. You should use this approach if you want to support old browsers.

To learn more about JavaScript arrays and how to store multiple pieces of information in one single variable, read this guide.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.