Why does this work (returns “one, two, three”):
var words = ['one', 'two', 'three'];
$("#main").append('<p>' + words.join(", ") + '</p>');
and this work (returns “the list: 111”):
var displayIt = function() {
return 'the list: ' + arguments[0];
}
$("#main").append('<p>' + displayIt('111', '222', '333') + '</p>');
but not this (returns blank):
var displayIt = function() {
return 'the list: ' + arguments.join(",");
}
$("#main").append('<p>' + displayIt('111', '222', '333') + '</p>');
What do I have to do to my “arguments” variable to be to use .join() on it?
It doesn’t work because the
argumentsobject is not an array, although it looks like it. It has nojoinmethod:To convert
argumentsto an array, you can do:Now
joinwill work:Alternatively, you can use jQuery’s
makeArray, which will try to turn “almost-arrays” likeargumentsinto arrays:Here’s what the Mozilla reference (my favorite resource for this sort of thing) has to say about it: