Is there a way to pass an unknown number of arguments like:
var print_names = function(names) {
foreach(name in names) console.log(name); // something li
You can use the spread/rest operator to collect your parameters into an array and then the length of the array will be the number of parameters you passed:
function foo(...names) {
console.log(names);
return names;
}
console.log(foo(1, 2, 3, 4).length);
Using BabelJS I converted the function to oldschool JS:
"use strict";
function foo() {
for (var _len = arguments.length, names = new Array(_len), _key = 0; _key < _len; _key++) {
names[_key] = arguments[_key];
}
console.log(names);
return names;
}