views:

30

answers:

3

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?

+2  A: 

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

Arun P Johny
A: 

In javascript, I think you want:

var array = new Array(1, 2, 3);
codersarepeople
`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 ;)
no
A: 

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.

no