Allowing an Array into a Function

I’m attempting to write a function that will do something like this.

sortProducts(1, 4, 3, 8, 4)

And then it will output the HTML of each product (Product 1, Product 4, Product 3)

In the order in which they are put into the function.

My question is what is the syntax for accepting an array of values like that?


function sortProducts(array variable){
do function in here.

inside your function sortProduct array variable ‘arguments’ will have all passed arguments into the function. So, you can just loop through it.
You don’t need specify any arguments in function definition for this to work.

Yeah you are right, I can do the following

sortProducts([3, 4, 5]);

And it automatically casts it to an Array.

:slight_smile: it’s actually was not what I meant, but if you can pass array into the function - it’s great.
In situation when you do need to pass not known number of arguments and access it in the function this should work for you:

someFunction(3, 4, 5);

function someFunction()

alert should print “3,4,5”, since it has each argument as array item of arguments array.