Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a new array to a function?

This is bad practice, I know. It's just for testing, but I can't seem to get it to work. Can I pass a new, initialized array to a JavaScript function?

I'm looking for the JavaScript equivalent of this C# method:

public void MyFunction(new string[]{"1","2","3"})
{    
}

I tried this, but to no avail:

function MyFunction(new array ('1','2','3')) {    
}
like image 659
Yatrix Avatar asked Dec 26 '22 20:12

Yatrix


1 Answers

It's easier than that:

  function myFunction( arr ) { ... }

Then:

  myFunction([1, 2, 3, "hello"]);

In function declarations there is no type information necessary (or possible). Just list out the parameter names. Then, when you call the function, you can use the array literal notation (just like you can in any expression to instantiate an array).

like image 161
Pointy Avatar answered Jan 06 '23 21:01

Pointy