1

I want to create a function with dynamic number of parameters. I have a string array like

['param1','param2','param3']

and i need to create a function with this array

function test(param1,param2,param3){

}

is there a way to do this?

0

2 Answers 2

3

You can use ...args ( rest parameter ) to collect dynamic number of parameters

function test(...args){
  console.log(args)
}
test(1,2,3,4)
test(1,2,3)

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this before but i guess i did it wrong. It's working now thanks
0

Just pass the array as a parameter

var a=['param1','param2','param3']
function test(a){
console.log(a)
}
test(a)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.