6

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?

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
  • 1
    I get the full "day" see in jsFiddle http://jsfiddle.net/fZbes/ – Jayantha Lal Sirisena Nov 19 '13 at 04:21
  • http://www.w3schools.com/jsref/prop_win_status.asp When you set that aray, what you are really doing is this: status = "day,dusk,night,dawn". window.status can not be set to an array. Javascript. – ioan Nov 19 '13 at 04:22
  • might be somewhere between init and debug, your code killed the array and convert it in a string? can you debug it just after initialization? – Ruben Kazumov Nov 19 '13 at 04:24
  • Perhaps you have another variable named status elsewhere, or your overriding your variable? – Brandon Boone Nov 19 '13 at 04:25
  • 1
    @Jayantha yes because jsfiddle's window doesn't contain status. Try doing it yourself in a Chrome console for example. – ioan Nov 19 '13 at 04:25
  • https://developer.mozilla.org/en-US/docs/Web/API/Window.status – Felix Kling Nov 19 '13 at 04:25
  • 4
    @Gamster: It's because the code in the fiddle is not executed in global scope. – Felix Kling Nov 19 '13 at 04:26
  • There are no other variables named status in my code, and it is logged right after initialization. – Sterling Archer Nov 19 '13 at 04:27
  • Because you are running in another scope, as Felix said. Click the links. – ioan Nov 19 '13 at 04:29
  • 3
    So we already established that `window.status` is a special variable. It was used to the set the text of the status text of a website, which browsers don't even show anymore nowadays. Obviously that means that the value of that variable can only be a string and that's why any value you assign to it is implicitly converted to a string. – Felix Kling Nov 19 '13 at 04:36

4 Answers4

6

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'
user229044
  • 232,980
  • 40
  • 330
  • 338
2

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);)

vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • *"Because it will be saving your array as a flat string"* But why? :) – Felix Kling Nov 19 '13 at 04:27
  • @FelixKling 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)`) – vol7ron Nov 19 '13 at 04:40
  • Yes I know. I think you should make this part of your answer. – Felix Kling Nov 19 '13 at 04:42
0

When you do

status = [...];

You are essentially writing or better stated, modifying the status variable on the window object.

window.status = [...];

If status is set as a new variable: var status = [...] then this will resolve the issue. I know you have var status in your example above but without it is the only way I can think of that would cause the issue.

Update

As Felix Kling states, the variable, being defined in a global scope and not encapsulated, will face this issue since it is a member variable on the window object.

I would suggest changing the name of the variable, or encapsulating it.

brenjt
  • 15,997
  • 13
  • 77
  • 118
  • That's incorrect. In global scope it doesn't matter whether you use `var` or not. – Felix Kling Nov 19 '13 at 04:26
  • He *is* typing `var status`. If he does so at global scope, this will **not** fix his issue. – user229044 Nov 19 '13 at 04:26
  • try doing it without the var, it does the exact behavior that he described. – brenjt Nov 19 '13 at 04:27
  • Yes, but so does including `var`. – Felix Kling Nov 19 '13 at 04:29
  • No it doesn't jsfiddle.net/fZbes – brenjt Nov 19 '13 at 04:30
  • Because your are not running the code in global scope: http://jsfiddle.net/ehcc5/. FWIW, it *does* seem to make a difference in Firefox though. In Firefox, using `var` and not using `var` actually yields different results. In Chrome it doesn't make a difference. But if you look at the OP's code, they *are* using `var`, so we can assume that they don't use Firefox. – Felix Kling Nov 19 '13 at 04:31
  • Yeah I am seeing what you mean now with the global scope. I didn't know that it would work differently in or out with `var` defined. – brenjt Nov 19 '13 at 04:34
-1

You have reassigned the 'status' variable to a string that begins with 'd' (i.e. day, dusk, or dawn).

var status = 'day';
console.log(status[0]) // d
console.log(status[1]) // a 
// etc...

Strings are treated as arrays of characters so you are accessing the individual characters with the brackets.

Rob Dawley
  • 244
  • 3
  • 5