How to convert arguments object into an array in JavaScript ?
Last Updated :
15 Sep, 2022
The arguments object is an array-like object that represents the arguments passed in when invoking a function. This array-like object does not have the array prototype chain, hence it cannot use any of the array methods. This object can be converted into a proper array using two approaches:
Method 1: Using the Array.from() method: The Array.from() method can be used to create a new array that is a shallow-copy of an array-like or iterable object. It contains three parameters that consist array-like object to be converted, an optional map function that can be called on every element of the array and a "this" argument that can be used with the map function. The arguments object is passed as an argument to this method and it will return the array form of the object. This array has the array prototype chain and can be used with the various array methods.
Syntax:
argumentsArray = Array.from(arguments)
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to convert arguments object into
an array in JavaScript?
</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
How to convert arguments object into
an array in JavaScript?
</b>
<p>
Click on the button to convert the
arguments object to an Array
and also use the Array join() method.
</p>
<p>The arguments passed are '1, "Two", 3'</p>
<button onclick="convertToArray(1, 'Two', 3)">
Convert to Array
</button>
<script type="text/javascript">
function convertToArray(a, b, c) {
console.log("The arguments object:")
console.log(arguments);
argumentsArray = Array.from(arguments);
console.log("The arguments object as array:")
console.log(argumentsArray);
// Example of using the join() method
joinedArray = argumentsArray.join(', ');
console.log("Using join() on array: ",
joinedArray);
}
</script>
</body>
</html>
Output:


Method 2: Using Array.prototype.slice() method: The Array.prototype.slice() method is used to return a portion of an array into a new array object. This array would be a shallow copy of the original array. This method has two optional arguments that indicate the beginning and ending index of the array. If the arguments are omitted, the indices are set to the first and last index of the array. This method can be used to convert the arguments object by binding this method to the object. The binding is done using the call() function and passing the array-like object as an argument to it. It will return an array form of the arguments object. This array has the array prototype chain and can be used with the various array methods.
Syntax:
argumentsArray = Array.prototype.slice.call(arguments)
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to convert arguments object
into an array in JavaScript?
</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
How to convert arguments object into
an array in JavaScript?
</b>
<p>
Click on the button to convert the
arguments object to an Array and
also use the Array join() method.
</p>
<p>The arguments passed are '1, "Two", 3'</p>
<button onclick="convertToArray(1, 'Two', 3)">
Convert to Array
</button>
<script type="text/javascript">
function convertToArray(a, b, c) {
console.log("The arguments object:")
console.log(arguments);
argumentsArray =
Array.prototype.slice.call(arguments);
console.log("The arguments object as array:")
console.log(argumentsArray);
// Example of using the join() method
joinedArray =
argumentsArray.join(', ');
console.log("Using join() on array: ",
joinedArray);
}
</script>
</body>
</html>
Output:


Method#3 : Using Spread Opertor: Spread operator allows an iterable to expand in place where 0+ argument are expected. This operator expands the arguments in place and we use square brackets to capture them and make array with them. We will expand the arguments object in place and form array with them.
Syntax:
var Array_obj = [...arguments];
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to convert arguments object into
an array in JavaScript?
</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
How to convert arguments object into
an array in JavaScript?
</b>
<p>
Click on the button to convert the
arguments object to an Array
and also use the Array join() method.
</p>
<p>The arguments passed are '1, "Two", 3'</p>
<button onclick="convertToArray(1, 'Two', 3)">
Convert to Array
</button>
<script type="text/javascript">
function convertToArray(a, b, c) {
console.log("The arguments object:")
console.log(arguments);
argumentsArray = [...argument];
console.log("The arguments object as array:")
console.log(argumentsArray);
// Example of using the join() method
joinedArray = argumentsArray.join(', ');
console.log("Using join() on array: ", joinedArray);
}
</script>
</body>
</html>
Output:
Spread operator
Spread operator
Method#4 : Using Object.values() method: The Object.values method is used to make an array whose value comes from the value of object.
Syntax:
var Array_obj = Object.values( obj );
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to convert arguments object into
an array in JavaScript?
</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
How to convert arguments object into
an array in JavaScript?
</b>
<p>
Click on the button to convert the
arguments object to an Array
and also use the Array join() method.
</p>
<p>The arguments passed are '1, "Two", 3'</p>
<button onclick="convertToArray(1, 'Two', 3)">
Convert to Array
</button>
<script type="text/javascript">
function convertToArray(a, b, c) {
console.log("The arguments object:")
console.log(arguments);
argumentsArray = Object.values(arguments);
console.log("The arguments object as array:")
console.log(argumentsArray);
// Example of using the join() method
joinedArray = argumentsArray.join(', ');
console.log("Using join() on array: ",
joinedArray);
}
</script>
</body>
</html>
Output:
Object.values method
Object.values method
Similar Reads
How to Convert an Object into Array of Objects in JavaScript? Here are the different methods to convert an object into an array of objects in JavaScript1. Using Object.values() methodObject.values() method extracts the property values of an object and returns them as an array, converting the original object into an array of objects.JavaScriptconst a = { java:
3 min read
JavaScript - Convert an Array to an Object These are the following ways to convert an array to an Object:1. Using JavaScript Object.assign() method The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.JavaScriptlet a = [1, 2, 3,
2 min read
How to Convert Object to Array in JavaScript? In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such
4 min read
Convert Dictionary into an Array of objects in JavaScript In JavaScript, dictionaries also known as objects are used to store key-value pairs. But sometimes you might need to convert this data into an array of objects so that you can handle or utilize particular parts more easily. Below are the multiple ways of converting a Dictionary into an Array of obje
4 min read
JavaScript - Convert Two-Dimensional Array Into an Object Here are the different methods to convert the two-dimensional array into an object in JavaScript.1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay
2 min read
How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one
2 min read
How to convert a map to array of objects in JavaScript? A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods
6 min read
How to Push an Object into an Array using For Loop in JavaScript ? JavaScript allows us to push an object into an array using a for-loop. This process consists of iterating over the sequence of values or indices using the for-loop and using an array manipulation method like push(), to append new elements to the array. We have given an empty array, and we need to pu
3 min read
JavaScript- Convert an Object to JS Array Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). Methods to convert the Objects to JavaScript Array:1. Using Obje
3 min read
How to convert Object's array to an array using JavaScript ? Given an array of objects and the task is to convert the object values to an array with the help of JavaScript. There are two approaches that are discussed below: Approach 1:We can use the map() method and return the values of each object which makes the array. Example: html<!DOCTYPE HTML>
2 min read