REW

How To Convert Arguments Into An Array?

Published Aug 29, 2025 5 min read
On this page

You can convert function arguments into an array using language-specific syntax or methods, with the best approach depending on the programming language being used.

Modern languages have simplified this process with features like rest parameters (JavaScript, Python), spread syntax (JavaScript), and dedicated functions such as Array.from().

JavaScript

In JavaScript, functions have an arguments object, which is "array-like" but not a true array. It has a length property and can be indexed, but lacks built-in array methods like map() and forEach(). The modern approach is to use rest parameters.

Modern methods (ES6+)

Rest parameters (...)

The most modern and recommended method is to use rest parameters, which collect all remaining arguments into a single standard array. This is the clearest and most concise option.

Syntax:

function myFunction(arg1, ...argsArray) {
  // `argsArray` is a real array containing all other arguments
  console.log(argsArray);
}
myFunction(1, 2, 3, 4); // Output: [2, 3, 4]

Use code with caution.

Spread syntax (...)

The spread syntax can be used to convert the arguments object into a new array.

Syntax:

function myFunction() {
  const args = [...arguments];
  console.log(args);
}
myFunction(1, 2, 3); // Output: [1, 2, 3]

Use code with caution.

Array.from() method

This method creates a new, shallow-copied Array instance from an array-like or iterable object.

Syntax:

function myFunction() {
  const args = Array.from(arguments);
  console.log(args);
}
myFunction('a', 'b', 'c'); // Output: ['a', 'b', 'c']

Use code with caution.

Legacy method (pre-ES6)

Array.prototype.slice.call()

This older technique works by using the call method to apply the slice method of the Array prototype to the array-like arguments object.

Syntax:

function myFunction() {
  const args = Array.prototype.slice.call(arguments);
  console.log(args);
}
myFunction(10, 20, 30); // Output: [10, 20, 30]

Use code with caution.

Python

Python functions can collect an arbitrary number of arguments into a tuple using a special syntax. While not an array, a tuple can be easily converted into a list if a mutable data structure is needed.

Using *args

The *args syntax in a function definition collects all positional arguments into a tuple named args.

Syntax:

def my_function(*args):
  # `args` is a tuple
  print(args)
  # Convert to a list if needed
  args_list = list(args)
  print(args_list)
my_function(1, "hello", [3, 4]);
# Output: (1, 'hello', [3, 4])
# Output: [1, 'hello', [3, 4]]

Use code with caution.

Using **kwargs

For keyword arguments, the **kwargs syntax collects them into a dictionary.

Syntax:

def my_function(**kwargs):
  # `kwargs` is a dictionary
  print(kwargs)
my_function(first=1, second="hello", third=[3, 4]);
# Output: {'first': 1, 'second': 'hello', 'third': [3, 4]}

Use code with caution.

C++

In C++, converting function arguments to an array requires modern C++ (C++11 and later) and involves variadic templates. This approach uses templates to process a variable number of arguments at compile time.

Using variadic templates with std::array

This method requires building a function template that accepts a parameter pack and uses it to construct a std::array.

Syntax:

#include <iostream>
#include <array>
#include <utility>
template<typename T, typename ... Args>
std::array<T, 1 + sizeof...(Args)> args_to_array(T&& head, Args&& ... args) {
  return std::array<T, 1 + sizeof...(Args)>{ std::forward<T>(head), std::forward<Args>(args)... };
}
int main() {
  auto my_array = args_to_array(1, 2, 3, 4);
  for (const auto& val : my_array) {
    std::cout << val << " ";
  }
  return 0;
}
// Output: 1 2 3 4

Use code with caution.

Using variadic templates with std::vector

For situations where the number of arguments is not known at compile time, a dynamic array like std::vector is a better fit.

Syntax:

#include <iostream>
#include <vector>
template<typename T, typename ... Args>
std::vector<T> args_to_vector(T&& head, Args&& ... args) {
  std::vector<T> result;
  result.reserve(1 + sizeof...(args));
  result.push_back(std::forward<T>(head));
  (result.push_back(std::forward<Args>(args)), ...); // Fold expression (C++17)
  return result;
}
int main() {
  auto my_vector = args_to_vector(1, 2, 3, 4);
  for (const auto& val : my_vector) {
    std::cout << val << " ";
  }
  return 0;
}
// Output: 1 2 3 4

Use code with caution.

Java

In Java, the equivalent of converting arguments to an array is the use of varargs, a feature that allows a method to accept a variable number of arguments of a specified type.

Using varargs (...)

When a method is declared with a varargs parameter, it effectively receives these arguments as an array inside the method body.

Syntax:

public class MyClass {
  public static void main(String[] args) {
    printNames("Alice", "Bob", "Charlie");
  }
  public static void printNames(String... names) {
    // `names` is already an array of Strings
    for (String name : names) {
      System.out.println(name);
    }
  }
}
// Output:
// Alice
// Bob
// Charlie

Use code with caution.

Analysis and best practices

The method for converting arguments to an array is highly dependent on the programming language and its version.

  • For JavaScript, rest parameters are the most idiomatic and modern approach. They offer clear, readable code and work directly with true arrays. Legacy methods involving the arguments object are generally discouraged, especially in newer code.
  • For Python, *args and **kwargs are fundamental language features for handling variadic functions. They produce tuples or dictionaries, which are the standard Pythonic way to handle collections of arguments. Conversion to a list is a simple one-line operation.
  • For C++, variadic templates provide a powerful, compile-time solution for creating arrays from variable arguments. The choice between std::array (for compile-time size) and std::vector (for dynamic size) depends on the specific performance requirements and flexibility needed.
  • For Java, varargs is the standard and simplest mechanism for accepting a variable number of arguments. The arguments are automatically available as an array, making the conversion trivial.

Performance considerations

  • JavaScript: Rest parameters and Array.from() are generally performant and optimized by modern JavaScript engines. Older methods like slice.call(arguments) may inhibit engine optimizations.
  • Python: The creation of a tuple via *args is very efficient. Conversion to a list involves a small, constant overhead.
  • C++: The compile-time expansion of variadic templates for std::array is highly efficient, with zero runtime cost. Using std::vector involves a dynamic allocation, which has a small overhead but offers greater flexibility.
  • Java: Varargs are a highly optimized language feature, with the array creation handled efficiently by the Java Virtual Machine.

Use case and complexity

The need to convert arguments to an array typically arises in functions that need to:

  • Accept a variable number of inputs, like a sum() function that can take any number of numbers.
  • Use array-specific methods on the arguments, such as map(), filter(), or sort().
  • Pass the arguments to another function that specifically expects an array.

Understanding the strengths and weaknesses of each approach allows developers to choose the most readable, performant, and idiomatic solution for their specific language and use case.

Enjoyed this article? Share it with a friend.