up vote 5 down vote favorite

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?

link|flag

2 Answers

up vote 9 down vote accepted

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);
})();
link|flag
Doh! Window.status is a property. I'll rename the variable. Thanks! – Sid Ahuja Jun 21 at 6:26
up vote 6 down vote

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

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.