Skip to content
the poor coder

convert arguments to array javascript

Poor Man
Published
Reading time
1 min read
On this page

How to Convert Arguments to Array in JavaScript#

If you want to convert the arguments passed to a function into an array in JavaScript, you can use the built-in Array.from() method or the spread operator (...). Let's take a look at both methods:

Using Array.from()#

Text

function myFunction() {
  const argsArray = Array.from(arguments);
  console.log(argsArray);
}

myFunction('hello', 123, true); // Output: ['hello', 123, true]
  

In the example above, we define a function called myFunction() and pass in three arguments. Inside the function, we use the Array.from() method to create an array from the arguments object. We then log the resulting array to the console. The output will be an array containing the three arguments passed to the function.

Using the Spread Operator#

Text

function myFunction() {
  const argsArray = [...arguments];
  console.log(argsArray);
}

myFunction('hello', 123, true); // Output: ['hello', 123, true]
  

In this example, we define the same function as before, but this time we use the spread operator (...) to convert the arguments object to an array. We then log the resulting array to the console. The output will be the same as before.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC