Categories
JavaScript Answers

How to convert the arguments object to an array in JavaScript?

Spread the love

To convert the arguments object to an array in JavaScript, we should replace it with the rest operator.

For instance, we write

const sortArgs = (...args) => {
  return args.sort((a, b) => {
    return a - b;
  });
};

to define the sortArgs function.

We use the spread operator to convert the arguments into an array with the ... rest operator.

args then has all the arguments we called sortArgs with.

And then we call args.sort to sort the arguments and returned a new array with the sorted values.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *