Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 512015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:16:35+00:00 2026-05-13T07:16:35+00:00

Is it possible to send a variable number of arguments to a JavaScript function,

  • 0

Is it possible to send a variable number of arguments to a JavaScript function, from an array?

var arr = ['a','b','c']

var func = function()
{
    // debug 
    alert(arguments.length);
    //
    for(arg in arguments)
        alert(arg);
}

func('a','b','c','d'); // prints 4 which is what I want, then 'a','b','c','d'
func(arr); // prints 1, then 'Array'

I’ve recently written a lot of Python and it’s a wonderful pattern to be able to accept varargs and send them. e.g.

def func(*args):
   print len(args)
   for i in args:
       print i

func('a','b','c','d'); // prints 4 which is what I want, then 'a','b','c','d'
func(*arr) // prints 4 which is what I want, then 'a','b','c','d'

Is it possible in JavaScript to send an array to be treated as the arguments array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:16:35+00:00Added an answer on May 13, 2026 at 7:16 am

    Update: Since ES6, you can simply use the spread syntax when calling the function:

    func(...arr);
    

    Since ES6 also if you expect to treat your arguments as an array, you can also use the spread syntax in the parameter list, for example:

    function func(...args) {
      args.forEach(arg => console.log(arg))
    }
    
    const values = ['a', 'b', 'c']
    func(...values)
    func(1, 2, 3)
    

    And you can combine it with normal parameters, for example if you want to receive the first two arguments separately and the rest as an array:

    function func(first, second, ...theRest) {
      //...
    }
    

    And maybe is useful to you, that you can know how many arguments a function expects:

    var test = function (one, two, three) {}; 
    test.length == 3;
    

    But anyway you can pass an arbitrary number of arguments…

    The spread syntax is shorter and “sweeter” than apply and if you don’t need to set the this value in the function call, this is the way to go.

    Here is an apply example, which was the former way to do it:

    var arr = ['a','b','c'];
    
    function func() {
      console.log(this); // 'test'
      console.log(arguments.length); // 3
    
      for(var i = 0; i < arguments.length; i++) {
        console.log(arguments[i]);
      }
    
    };
    
    func.apply('test', arr);
    

    Nowadays I only recommend using apply only if you need to pass an arbitrary number of arguments from an array and set the this value. apply takes is the this value as the first arguments, which will be used on the function invocation, if we use null in non-strict code, the this keyword will refer to the Global object (window) inside func, in strict mode, when explicitly using ‘use strict’ or in ES modules, null will be used.

    Also note that the arguments object is not really an Array, you can convert it by:

    var argsArray = Array.prototype.slice.call(arguments);
    

    And in ES6:

    const argsArray = [...arguments] // or Array.from(arguments)
    

    But you rarely use the arguments object directly nowadays thanks to the spread syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 281k
  • Answers 281k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ok, I found a solution. Not sure this is the… May 13, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer The best way to perform text search in an HTML… May 13, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer I agree, you're better off going with 100% CRM. If… May 13, 2026 at 3:51 pm

Related Questions

I'm writing a function in C that takes a variable number of arguments. size_t
I am implementing a roulette wheel selection, and I would like to keep as
I'm working on a VB6 application and I would like to send a Type
I have to write a script to send mails using unix shell scripts. The
We are loading an actionscript2 swf into an actionscript3 swf. as3 is embeded on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.