Javascript on Firefox 3 Slice Bug

April 5, 2009 § 3 Comments

See the code below:

container = ['localhost:8080', 'aaaaaaaaa', 'history']
for(var i in container)
{
console.log(container[i]);
console.log(container.slice(0,i+1));
}

The Firebug output is below:

localhost:8080
["localhost:8080"]
aaaaaaaaa
["localhost:8080", "aaaaaaaaa", "history"]
history
["localhost:8080", "aaaaaaaaa", "history"]

Notice the problem there? When i == 1, why slice() output is not:
["localhost:8080", "aaaaaaaaa"] ??????

I cannot explain why this is happening and yes… I have refreshed my browser multiple times.

Reference:

Mozilla Developer Center :: Slice

Advertisements

Tagged: ,

§ 3 Responses to Javascript on Firefox 3 Slice Bug

  • Vijay Dev says:

    If I do console.log(container.slice(0,2)), I get the correct output of [“localhost:8080”, “aaaaaaaaa”] in FireBug !

  • Vijay Dev says:

    After some debugging, I found out the flaw in the above code.

    When you use the following syntax of the “for” loop: for(var i in container), the variable “i” is of string data type. Used typeof and confirmed it. This causes i+1 to be 01, 11, 21, 31 and so on…

    You can use for(var i = 0 ; i<container.length; i++) to get the proper output. Or you can do a parseInt(i)+1 every time.

  • didip says:

    @Vijay
    Thanks for the reply! I DID NOT check the type as you suggested, once again, thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Javascript on Firefox 3 Slice Bug at RAPD.

meta

%d bloggers like this: