10

I am trying to do something really simple - initialize an array in Javascript. And it's not working in Google Chrome. Here is the code:

status = [];
for(i=0; i < 8; i++)
  status[i]=false;

alert(status.length); //It says 0 when it should say 8

What gives?

Braiam
  • 1
  • 11
  • 47
  • 78
tinkerr
  • 975
  • 2
  • 14
  • 32
  • 3
    You realize that `reason = reasonT;` will never get called because you `return` the line before it? – Jeff Shaver Apr 03 '13 at 13:32
  • @MattBurland I call `isReady(true)` or `isReady(false, "some string")` or just `isReady()` to check the actual values, but either way in the console `typeof status` without even having called `isReady` says is a string also @jeff thanks to pointing that out. – Braiam Apr 03 '13 at 13:38
  • Your code works for me: http://jsfiddle.net/hrbkk/1/ – gen_Eric Apr 03 '13 at 13:53
  • @RocketHazmat indeed it works in jsfiddle, but then, and here's the the awesomeness of chrome, why is keeping telling me that the `var status = true`is a string in the extension? Inside the function seems to works just fine. – Braiam Apr 03 '13 at 14:03
  • 2
    It works in the fiddle because `status` is a local variable and not global. – epascarello Apr 03 '13 at 14:07
  • @Bergi that question wouldn't be easily found with that title. If anything, this one is better scoped and has better title. – Braiam Nov 30 '16 at 13:12
  • @Bergi I'm going to propose the merge towards this one and put Oriol answer as accepted, would you close all questions you find against this one? – Braiam May 18 '20 at 11:59
  • 1
    @Braiam I don't remember the old comment history, but I think I was considering a merge the other way round. The other questions are older, shorter (simple code example), higher voted, or both. – Bergi May 18 '20 at 12:52
  • @Bergi Yet they are less discoverable than this one. Remember, people would be seeing those titles on their searches. This one has higher view counts in less time. – Braiam May 18 '20 at 13:04
  • @Bergi Propose a title. Also, it doesn't have Oriol answer. Are you sure? – Braiam May 18 '20 at 13:15
  • @Braiam I was assuming we were going to get them merged, so it doesn't matter if it has the answer right now. But we could also merge into the one with Oriol's answer, I don't care. Not certain whether we should distinguish between the type of value people try to assign to the variable, or have a single canonical only. Regarding the title, I'd opt for something like "*`var status` does not accept assignments and keeps coming up as string*". – Bergi May 18 '20 at 13:30

3 Answers3

14

The assignment of your status variable, clashes with the window.status property.

Chrome simply refuses to make the assignment.

The window.status property, sets or gets the text in the status bar at the bottom of the browser.

I would recommend you to either, rename your variable or use an anonymous function to create a new scope, also remember to always use var for declaring variables:

(function () {
  var status = [];

  for (var i = 0; i < 8; i++)
    status[i] = false;

  alert(status.length);
})();
Etheryte
  • 24,589
  • 11
  • 71
  • 116
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
8

Change the variable name. Seems like status is a property of window, and Chrome makes it inmutable . I didn't expect that, too.

Chubas
  • 17,823
  • 4
  • 48
  • 48
6

The problem here is what status is attached to. You are using it off the global/window scope.

Back in the good ole days we were able to set the text in the status bar. How you would do it is by setting window.status to a string value. So what you are doing is NOT setting a variable, but changing the string of the browser's status bar.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • ... I totally forgot about `window.status`. Man, am I glad it doesn't work anymore, people used to abuse that! This might very well be what's going on here. – gen_Eric Apr 03 '13 at 14:05
  • We all did... I've changed the variable to status_ready and now works as expected. – Braiam Apr 03 '13 at 14:10