How to Create an Array From Multiple Arguments in JavaScript

Create an Array From Multiple Arguments in JavaScript

In JavaScript, you may find yourself wanting to take multiple values—like "apple", "banana", and "cherry"—and wrap them together into a single array. Whether you're passing values to a function, processing input dynamically, or just grouping items on the fly, there are multiple clean and expressive ways to convert multiple arguments into an array.

Let’s explore a few beginner-friendly methods to handle this task using modern JavaScript techniques.

Method 1: Using Array.of()

The Array.of() method is tailor-made for this purpose. It creates a new array instance from a variable number of arguments—each argument becomes an element in the array, just as you’d expect.

const fruits = Array.of("apple", "banana", "cherry");
console.log(fruits);
["apple", "banana", "cherry"]

Method 2: Using the Spread Operator in a Function

When values come in through a function—especially when their number is unknown in advance—you can use the rest parameter syntax ...args. This gathers all passed arguments into a real array inside the function.

function collectItems(...items) {
  return items;
}

const result = collectItems("apple", "banana", "cherry");
console.log(result);
["apple", "banana", "cherry"]

Method 3: Using arguments Object in Traditional Functions

In older or more traditional JavaScript code, you might come across the arguments object inside a regular function. It’s an array-like object, and with a little nudge—like Array.from()—you can turn it into a proper array.

function buildArray() {
  return Array.from(arguments);
}

const items = buildArray("apple", "banana", "cherry");
console.log(items);
["apple", "banana", "cherry"]

Method 4: Using the Spread Operator With Existing Values

If your values are already stored in variables, or if you want to combine values from multiple arrays or items, the spread operator can help. Just wrap the values in square brackets and use ... to include them.

const fruit1 = "apple";
const fruit2 = "banana";
const others = ["cherry", "date"];

const allFruits = [fruit1, fruit2, ...others];
console.log(allFruits);
["apple", "banana", "cherry", "date"]

Which Method Should You Use?

It depends on context. If you're calling a function with multiple values and want to instantly capture them as an array, go with ...rest. If you're writing modern code and prefer readability, Array.of() is crystal-clear. For legacy environments or older codebases, arguments still works just fine. And if you’re merging values from different places, spread syntax is your go-to tool.

Final Thoughts

Being able to take multiple arguments and group them into an array is a practical skill you’ll use time and time again. From building dynamic lists to writing utility functions, it makes your code flexible, reusable, and easy to scale.

Modern JavaScript offers beautiful syntax for this task. The key is knowing when to use each method and how they shape the final array. So the next time you're working with values like "apple", "banana", and "cherry"—you'll know exactly how to bundle them together with style and intent.

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...