This has been incredibly frustrating, especially given the simplicity of it. Simple javascript:
<script type="text/javascript">
var name = new Array("cat","dog");
document.write(name[1]);
</script>
This script prints: “a” (as in the “a” from “cat”…aka name[0][1]…).
Yet when I change it to document.write(name), it prints the whole array (i.e. “cat,dog”). Why for the love of god is this simple array failing to print the entire string (which should be “dog”)?!
The global variable
namerefers to the existingwindow.nameproperty, which was used to set the name of the browser window. It converts any value assigned to it to a string:If you change the variable name to e.g.
namesit will do as expected.Using
namein function scope scope will work fine of course.