Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes the difference in this toString method?

Tags:

javascript

I'm mainly interested in the Object.prototype.toString method. Both are operating on arrays but the Object.prototype.toString does something different to the output. Why is this?

Outputs: 1, 2, 3

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

Outputs: blank

console.log([].toString());

Outputs: [object Array]

return Object.prototype.toString.apply([]); 
like image 558
runners3431 Avatar asked Mar 18 '23 15:03

runners3431


1 Answers

Because Array.prototype.toString is a different function to Object.prototype.toString.

The designers of the function decided to make Array stringification output the data in the array.

like image 104
Quentin Avatar answered Mar 21 '23 05:03

Quentin