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

  • SEARCH
  • Home
  • 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 6569207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:35:50+00:00 2026-05-25T14:35:50+00:00

Background: I came across a very strange phenomenon while working with a node list.

  • 0

Background:


I came across a very strange phenomenon while working with a node list. I wanted to use getElementsByClassName or something similar and then sort it. I decided one way would be to iterate through the nodelist and push each item to an array and sort the array. (This did work by the way but not as expected). I tried using the for (var i in nodeList) to iterate through, but it kept throwing an exception on the last few items, which were undefined. the weird part is I could instead use for (var i = 0; i < nodeList.length; i++) to iterate through. I just tested it again and on a stackoverflow page I ran in my console the following code:

for (var i in document.getElementsByTagName("span"))
    console.count("items");
console.log(document.getElementsByTagName("span").length);

It counted out to items: 382 but length gave 380. As expected, when I entered document.getElementsByTagName("span")[380] and document.getElementsByTagName("span")[381] they came back undefined. This strange behavior does not occur on arrays (granted, nodeLists and arrays are different, but this does prove that it’s not the different for loops causing the issue).

question:


Why does for(var i in nodeList) constructs behave differently on nodeLists returning a couple of undefined items at the end?

  • 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. Editorial Team
    Editorial Team
    2026-05-25T14:35:51+00:00Added an answer on May 25, 2026 at 2:35 pm

    The two additional properties that the for in iteration statement catches are:

    • length
    • item

    Let me give you a simple example. Let’s say that there are 3 SPAN elements on the page.

    var spans = document.getElementsByTagName( 'span' );
    

    Now, spans is a NodeList object which contains 5 properties:

    • 0
    • 1
    • 2
    • length
    • item

    The first 3 properties are indexes and they contain references to those SPAN elements. The other two properties – length and item – are two additional properties. All NodeList objects have these two properties.

    The for in statement iterates over all 5 properties of the NodeList object, which is probably not what you want. Therefore, use a regular for statement.

    var i, span;
    
    for ( i = 0; i < spans.length; i++ ) {
        span = spans[i];
        // do stuff with span
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While debugging a WCF service I came across this issue. I use an HttpURLConnection
Background I came from several years working in a company where all the database
I'm from a C background and understand basics of printf function. I came across
Today I came across a very nasty problem, I need to make the front-end
Background I recently came across the fuppes UPnP media server and it seemed great.
I'm working on a web site with Expression Web. I came across this website
Creating a mini-database with access, i came across this problem: For the background, i
fellow coders, just came across a strange (at least to me) behaviour for the
I'm new to Ruby - came from Java background. I want to use third
I came across this background image in a CSS file. How can I view

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.