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 509291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:59:44+00:00 2026-05-13T06:59:44+00:00

I used JSLint on a JavaScript file of mine. It threw the error: for(

  • 0

I used JSLint on a JavaScript file of mine. It threw the error:

for( ind in evtListeners ) {

Problem at line 41 character 9: The body of a for in should be
wrapped in an if statement to filter unwanted
properties from the prototype.

What does this mean?

  • 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-13T06:59:45+00:00Added an answer on May 13, 2026 at 6:59 am

    First of all, never use a for in loop to enumerate over an array. Never. Use good old for(var i = 0; i<arr.length; i++).

    The reason behind this is the following: each object in JavaScript has a special field called prototype. Everything you add to that field is going to be accessible on every object of that type. Suppose you want all arrays to have a cool new function called filter_0 that will filter zeroes out.

    Array.prototype.filter_0 = function() {
        var res = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i] != 0) {
                res.push(this[i]);
            }
        }
        return res;
    };
    
    console.log([0, 5, 0, 3, 0, 1, 0].filter_0());
    //prints [5,3,1]
    

    This is a standard way to extend objects and add new methods. Lots of libraries do this.
    However, let’s look at how for in works now:

    var listeners = ["a", "b", "c"];
    for (o in listeners) {
        console.log(o);
    }
    //prints:
    //  0
    //  1
    //  2
    //  filter_0
    

    Do you see? It suddenly thinks filter_0 is another array index. Of course, it is not really a numeric index, but for in enumerates through object fields, not just numeric indexes. So we’re now enumerating through every numeric index and filter_0. But filter_0 is not a field of any particular array object, every array object has this property now.

    Luckily, all objects have a hasOwnProperty method, which checks if this field really belongs to the object itself or if it is simply inherited from the prototype chain and thus belongs to all the objects of that type.

    for (o in listeners) {
        if (listeners.hasOwnProperty(o)) {
           console.log(o);
        }
    }
     //prints:
     //  0
     //  1
     //  2
    

    Note, that although this code works as expected for arrays, you should never, never, use for in and for each in for arrays. Remember that for in enumerates the fields of an object, not array indexes or values.

    var listeners = ["a", "b", "c"];
    listeners.happy = "Happy debugging";
    
    for (o in listeners) {
        if (listeners.hasOwnProperty(o)) {
           console.log(o);
        }
    }
    
     //prints:
     //  0
     //  1
     //  2
     //  happy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lengthy JavaScript file that passes JSLint except for used before it
I have read Crockford's JavaScript: The Good Parts and have used his validator JSLint
jslint reports message Insecure '.'. at line html = /<body.*?>([\s\S]*)<\/body>/.exec(responseText); How to fix this
I've overcome a problem of JSLint showing me a X was used before it
I used the phonegap website to create an IPA file. Now I want to
I used ReconstructMe to scan my first half body (arm and head). The result
When I am loading my page I include a javascript library that is used
I have used a PHP version of JSLint for the build script. But I
Possible Duplicate: JSLint: was used before it was defined I run JSlint and saw
I've been using JSLint to make me feel bad about my JavaScript. It is

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.