Skip to content

How to accept infinite parameters in a JavaScript function?

In this quick post, let’s take a look at how to accept infinite arguments inside a JavaScript function. We can extend the spread operator (…) in JavaScript to accept unlimited parameters in a JavaScript function.

function print(...args) {
  args.forEach((arg, index) => {
    console.log(arg);
  });
}

print('1', '2', 100, "Fourth");

//1
//2
//100
//Fourth

The above example accepts any number of arguments and prints them. We can also pass any type of parameters.

function print(...args) {
  args.forEach((arg, index) => {
    console.log(arg);
  });
}

print('First', 2, false, new Date());

//First
//2
//false
//Fri Jun 26 2020 08:32:07 GMT+0830 (Pacific Standard Time)
See also  Display console logs in HTML
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.