2

I'm tryig to create a array named status in my javascript, but it is not working in Google Chrome.

<html>
    <body>
        <script>
            var array = [1, 2, 3];
            document.write("Type of [" + array + "] : " + (typeof array) + "<br />");
            document.write("Value of array.length : " + array.length + "<br />");

            document.write("<br /><br />");

            var status = [1, 2, 3];
            document.write("Type of [" + status + "] : " + (typeof status) + "<br />");
            document.write("Value of status.length : " + status.length + "<br />");
            </script>
    </body>
</html>

In the above piece of code even though I'm assigning an array value to the variable status In chrome the value is considered as of type string.

Is it a bug with Chrome or a valid behaviour?

3 Answers3

2

The problem is that in Chrome, window (the global object) has a status property, which for some reason Chrome seems to be referring to instead of your status var. Renaming status to anything else, say, myStatus, will give you the results you expect.

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • I ran into this problem recently as well. I had var status = $('#status'); which worked fine in IE but broke in Chrome. The problem was that Chrome protects properties of window by going so far as even protecting the global variable names - so var location, var status, var navigator, etc all fail if declared globally in Chrome. They all work fine if declared inside of a function - so the workaround is (function() { /* put your code here */ })(); or use different variable names. Annoying for quick one-off scripts! – Chris Moschini Jan 17 '13 at 17:26
-1

This issue is already answered here.

<html>
    <body>
        <script>
            function test(){
                var array = [1, 2, 3];
                document.write("Type of [" + array + "] : " + (typeof array) + "<br />");
                document.write("Value of array.length : " + array.length + "<br />");

                document.write("<br /><br />");

                var status = [1, 2, 3];
                document.write("Type of [" + status + "] : " + (typeof status) + "<br />");
                document.write("Value of status.length : " + status.length + "<br />");
            }

            test();
            </script>
    </body>
</html>

If you try the above given code it works as expected because the scope of the variable status is limited to the local scope of the method status. In your sample the scope of the variable was global(scope is window).

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
-2

In javascript, I think you want:

var array = new Array(1, 2, 3);
codersarepeople
  • 1,971
  • 3
  • 20
  • 25
  • `var array = [1,2,3];` is fine too. Better, probably, because the `Array` ctor can be confusing... passing it one argument sets the initial size of the array, passing more means each argument is an array member... silly. BTW that -1 wasn't from me ;) – Dagg Nabbit Aug 15 '10 at 02:51