Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't .join() work with function arguments?

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?

like image 634
Edward Tanguay Avatar asked Jan 19 '10 04:01

Edward Tanguay


People also ask

What is function argument in JavaScript?

Function arguments are the real values passed to (and received by) the function.

Which keyword is used to access the array of arguments of a function?

You can access specific arguments by calling their index. var add = function (num1, num2) { // returns the value of `num1` console. log(arguments[0]); // returns the value of `num2` console.

What are arguments in a function?

In mathematics, an argument of a function is a value provided to obtain the function's result. It is also called an independent variable. For example, the binary function has two arguments, and , in an ordered pair . The hypergeometric function is an example of a four-argument function.

Which of the following methods takes arguments as an array in JS?

The apply() method calls the specified function with a given this value, and arguments provided as an array (or an array-like object).


2 Answers

It doesn't work because the arguments object is not an array, although it looks like it. It has no join method:

>>> var d = function() { return '[' + arguments.join(",") + ']'; } >>> d("a", "b", "c") TypeError: arguments.join is not a function 

To convert arguments to an array, you can do:

var args = Array.prototype.slice.call(arguments); 

Now join will work:

>>> var d = function() {   var args = Array.prototype.slice.call(arguments);   return '[' + args.join(",") + ']'; } >>> d("a", "b", "c"); "[a,b,c]" 

Alternatively, you can use jQuery's makeArray, which will try to turn "almost-arrays" like arguments into arrays:

var args = $.makeArray(arguments); 

Here's what the Mozilla reference (my favorite resource for this sort of thing) has to say about it:

The arguments object is not an array. It is similar to an array, but does not have any array properties except length. For example, it does not have the pop method. ...

The arguments object is available only within a function body. Attempting to access the arguments object outside a function declaration results in an error.

like image 192
John Feminella Avatar answered Oct 10 '22 01:10

John Feminella


If you are not interested on other Array.prototype methods, and you want simply to use join, you can invoke it directly, without converting it to an array:

var displayIt = function() {     return 'the list: ' + Array.prototype.join.call(arguments, ','); }; 

Also you might find useful to know that the comma is the default separator, if you don't define a separator, by spec the comma will be used.

like image 45
Christian C. Salvadó Avatar answered Oct 10 '22 02:10

Christian C. Salvadó