While creating a small counter based game, I had an array like this:
var status = ["day","dusk","night","dawn"];
If I tried to access the first index of the array, I would get:
console.log(status[0]); //yields "d"
@monners mentioned it might be a reserved word, so I changed the variable name to xstatus
and it worked fine.
My question is: why would status[0]
return only the first letter of the first index?
The following are more examples of reserved words. abstract , if , private , this , double , implements , throw , boolean , else , import , public , throws , break , return , byte , extends , int , short , true , false , case , interface , static , try , catch , final , long , void . Edit Me on GitHub!
Keywords have a special meaning in a language, and are part of the syntax. Reserved words are words that cannot be used as identifiers (variables, functions, etc.), because they are reserved by the language. In practice most keywords are reserved words and vice versa.
Further, reserved words may not be redefined by the programmer, but predefineds can often be overridden in some capacity. Languages vary as to what is provided as a keyword and what is a predefined. Some languages, for instance, provide keywords for input/output operations whereas in others these are library routines.
To escape reserved keywords in SQL SELECT statements and in queries on views, enclose them in double quotes ('').
You're modifying window.status
which cannot be set to an array:
https://developer.mozilla.org/en-US/docs/Web/API/Window.status
There is some unexplained behaviour in Firefox. While both status
and var status
at the global scope provide references to the window.status
property, var status
doesn't flatten the array:
status = ["meagar"];
console.log(window.status[0]); // 'm'
vs
var status = ["meagar"];
console.log(window.status[0]); // 'meagar'
Because it will be saving your array as a flat string and d
is the first character (position 0
) of the string.
I believe this goes years back into the old Navigator statusbar days (remember those ticker status bars). The status could only output as string --- arrays, when set to string, are flattened and comma-delimited (e.g. var ar=['foo','bar']; alert(ar);
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With