In JavaScript, you can pass any number of arguments to a function using the arguments object or by using the rest parameter syntax.
Using the arguments object:
function myFunction() { for (let i = 0; i < arguments.length; i++) { console.log(arguments[i]); }}myFunction('arg1', 'arg2', 'arg3');
function myFunction() {
for (let i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
myFunction('arg1', 'arg2', 'arg3');
In this example, the myFunction function can accept any number of arguments. Inside the function, you can access the arguments using the arguments object. It allows you to iterate over the arguments using a loop and access them by index.
function myFunction(...args) { for (let i = 0; i < args.length; i++) { console.log(args[i]); }}myFunction('arg1', 'arg2', 'arg3');
function myFunction(...args) {
for (let i = 0; i < args.length; i++) {
console.log(args[i]);
In this example, the myFunction function uses the rest parameter syntax (…args) to capture any number of arguments into an array named args. You can then iterate over the args array and access the individual arguments.
Both approaches allow you to pass any number of arguments to a function in JavaScript. Choose the one that suits your use case and coding style.