Your first example is correct as far as it goes, but since you're passing multiple arguments, you need to declare multiple parameters to receive them in, and use them:
var myArr = [];
function Items(item1, item2, item3) { // *** Note parameters
myArr.push(item1, item2, item3); // *** Using them
}
Items("item1", "item2", "item3");
console.log(myArr);
Alternately, you could pass in an array:
var myArr = [];
function Items(items) {
// ES2015+ spread notation
myArr.push(...items);
// Or ES5 and earlier:
// myArr.push.apply(myArr, items);
}
Items(["item1", "item2", "item3"]);
// ^-------------------------^---- note passing in an array
console.log(myArr);
More about spread notation here.
If you want to accept discrete arguments of any length, in ES2015+ you'd use a rest parameter:
var myArr = [];
function Items(...items) {
myArr.push(...items);
}
Items("item1", "item2", "item3");
console.log(myArr);
In ES5 and earlier, you'd use arguments
:
var myArr = [];
function Items() {
myArr.push.apply(myArr, arguments);
}
Items("item1", "item2", "item3");
console.log(myArr);