After reading this discussion and this article, I still have this question. Let’s say I have the following snippet:
var arr = new Array(3);
arr[0] = "Zero";
arr[1] = "One";
arr[2] = "Two";
document.write(arr.join(","));
If I replace the document.write() line with document.write(arr);, are they equivalent? Does the replacement statement automatically join array elements with a comma as the delimiter?
Thanks in advance!
“But I couldn’t figure out why”
This is because everything has a
toStringfunction as part of its prototype. When you write it out, this function is invoked to get the string representation of whatever it is. For arrays, the default handling is the same asjoin.