1

In the following JavaScript code snippet, I expect that the console output be 3 but in both Chrome and Mozilla, it is 23. Why?

var status = ["Busy", "Preferred", "Available"];        
console.log( status.length);
5
  • 2
    Maybe it's a bug that has to do with window.status? Commented Sep 11, 2015 at 1:35
  • 1
    Yes, whatever you assign to window.status gets converted into a string. Commented Sep 11, 2015 at 1:36
  • Yeah, window is implicit without using. Commented Sep 11, 2015 at 1:39
  • 1
    @ryanpcmcquen - In your JSfiddle, status isn't a global variable, but a local one inside the window.onload event handler function. Hence it works. Commented Sep 11, 2015 at 1:44
  • Good point @techfoobar, as Stefan Baiu said, OP should put this variable in some kind of function. Using global variables should be avoided if possible. Commented Sep 11, 2015 at 1:46

2 Answers 2

5

There's already a window.status (a global variable named status). Read more here.

When you're trying to set the array ["Busy", "Preferred", "Available"] to your global variable, the window.status setter will be invoked. So, window.status property will contain the string "Busy,Preferred,Available".

So, yeah change the variable name or don't use global variables (please).

Sign up to request clarification or add additional context in comments.

Comments

2

Looks like status is a reserved variable from Chrome and Firefox, try use another variable name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.