Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 180361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:34:23+00:00 2026-05-11T14:34:23+00:00

I was surprised when I was able to reproduce a bug with a minimum

  • 0

I was surprised when I was able to reproduce a bug with a minimum amount of code. Note that in this minimalist example Array.indexOf isn’t being called. Also note that I’ve tried several different implementations of indexOf, including several from stackoverflow.com.

The bug is, when the for…in executes in IE, three alerts are displayed: ‘indexOf’, ‘0’, and ‘1’. In FF, as one would expect, only two (‘0’, ‘1’) appear.

<html> <body onLoad='test();'> <script language='javascript'>    var testArray = ['Foo', 'Bar'];     if(!Array.prototype.indexOf) {       Array.prototype.indexOf = function (obj, fromIndex) {          if (fromIndex == null) {             fromIndex = 0;          } else if (fromIndex < 0) {             fromIndex = Math.max(0, this.length + fromIndex);          }          for (var i = fromIndex, j = this.length; i < j; i++) {             if (this[i] === obj)                return i;          }          return -1;       };    }     function test() {       var i;        for(i in testArray) {          alert(i);       }    } </script> </body> </html> 

Can anyone explain this? I’ve already changed my code to use a while so I’m not under the gun, but this one really has me stumped. It reminds me of memory overrun errors in c.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:34:23+00:00Added an answer on May 11, 2026 at 2:34 pm

    See ‘for in Intrigue‘ on the Yahoo! User Interface blog.

    The reason your code is working as expected in Firefox is because you haven’t added your own indexOf method in Firefox. The for in loop iterates over all the keys in the object’s prototype chain, including the indexOf method you added. Douglas Crockford suggests the following solution:

    for (var p in testArray) {     if (testArray.hasOwnProperty(p)) {         alert(testArray[i]);     } } 

    Alternatively, you can just filter out functions:

    for (var p in testArray) {     if (typeof testArray[p] !== 'function') {         alert(testArray[i]);     } } 

    Also, as ‘nickf’ points out, it is best not to use the for in loop for iterating over arrays. The for in loop is intended for iterating over the keys in an object.

    Steve

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Surprised that i havent been able to find this myself, but anyway. Let's say
I'm quite surprised I haven't been able to find out what characters I need
I was surprised to see in the Java source that System.arraycopy is a native
I am not able to compile the following code. I run it in a
I am surprised at how much difficulty I am having with this. I am
A method I work with that is called tens of thousands of times started
What I need is to be able to extract the files in a .rar
I've got a R script for which I'd like to be able to supply
By the looks of it, I wouldn't be surprised if Starting with Lift has
Firstly, I'm new to the specifics of IPv6 so I wouldn't be surprised if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.