After writing the following javascript code:
<script language="javascript" type="text/javascript">
var name = new Array();
var i = 0;
function doSomething() {
name[i] = "a";
alert(name[i]);
i++;
}
</script>
I encountered an issue where it alerted undefined
instead of a
in my Chrome browser. To troubleshoot, I tested alert(i)
and that worked properly.
Is there a way to properly assign values to a global array within a function?
Update: The problem was resolved by simply renaming the array, which fixed the issue. But what caused this solution to work?