Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't .join() function arguments - TypeError: undefined is not a function

Minimum example:

function test() {
  console.log(arguments.join(','));
}

test(1,2,3);

I then get:

TypeError: undefined is not a function

However, when I do the same for an array:

console.log([1,2,3].join(','));

I get

"1,2,3"

As expected.

What's wrong with arugments? It's suppose to be an array:

(function () {
  console.log(typeof [] == typeof arguments)
})();

true

like image 734
Jason Avatar asked Dec 29 '25 17:12

Jason


1 Answers

Arguments is not an array.

(function(){
   console.log(typeof arguments);
})();
// 'object'

It is an array-like structure with a length and numeric properties, but it is not actually an array. If you want to, you may use the array function on it though.

function test() {
    console.log(Array.prototype.join.call(arguments, ','));

    // OR make a new array from its values.
    var args = Array.prototype.slice.call(arguments);
    console.log(args.join(','));
}

test(1,2,3);

Note, your example works because array is not a type. typeof [] === 'object' also. You can however check if an object is an array by using

Array.isArray(arguments) // false
Array.isArray([]) // true
like image 73
loganfsmyth Avatar answered Jan 01 '26 07:01

loganfsmyth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!