Following is some codes and output from the Chrome Developers\' Console
Case 1:
var myarr = document.location.hostname.split(\".\");
James is right: because name
is a string property of window
, if you're executing this code in the global scope you're set that property not your variable. So, if you set an array, it set to window.name
the string version of that array (basically, array.toString()
that is the same of array.join()
).
Because it's a string – and not an array – using the square notation let you access to the single character based on a specific index. So:
var str = "ptamz";
str[0] // "p"
str[1] // "t"
It's equivalent to:
var str = "ptamz";
str.charAt(0) // "p"
str.charAt(1) // "t"