I have a function that can accept any number of arguments...
const getSearchFields = () => {
const joined = arguments.join(\'/\');
};
The problem is that arguments
is an array-like object (it has numbered properties mapping to the passed arguments and a length
property), but it actually isn't an array.
In the old days, you could make it into a real array with Array.prototype.slice.call(arguments)
(among other methods).
However, we're a bit luckier these days (depending on your supported platforms of course). You can either do...
const getSearchFields = (...args) => {
Array.isArray(args); // true
};
...or...
const getSearchFields = () => {
const args = Array.from(arguments);
Array.isArray(args); // true
};
The first example is preferred because...
arguments
is a magic variable (it's not defined explicitly, so where did it come from?)'use strict'
)You have to make assignment to a variable for it to work.
var a = ['Wind', 'Rain', 'Fire'];
var myVar1 = a.join();
var myVar2 = a.join(', ');
var myVar3 = a.join(' + ');
var myVar4 = a.join('');