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

后端 未结 12 728
[愿得一人]
[愿得一人] 2020-11-22 10:51

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()
{
    //         


        
12条回答
  •  渐次进展
    2020-11-22 11:03

    Yes you can pass variable no. of arguments to a function. You can use apply to achieve this.

    E.g.:

    var arr = ["Hi","How","are","you"];
    
    function applyTest(){
        var arg = arguments.length;
        console.log(arg);
    }
    
    applyTest.apply(null,arr);
    

提交回复
热议问题