I believe the answer you are looking for is linked in the accepted answer to the question you linked: http://www.jslint.com/help.html#forin

does not recommend use of the for in statement. Use Object.keys instead.

It goes on to explain why that is recommended.

The for in statement allows for looping through the names of all of the properties of an object. Unfortunately, it also loops through all of the properties that were inherited through the prototype chain. This has the bad side effect of serving up method functions when the interest is in data properties. If a program is written without awareness of this situation, then it can fail.

The body of every for in statement should be wrapped in an if statement that does filtering. It can select for a particular type or range of values, or it can exclude functions, or it can exclude properties from the prototype. For example,

for (name in object) {
    if (object.hasOwnProperty(name)) {
        ....
    }
}

Note that the above code will fail if the object contains a data property named hasOwnProperty. Use Object.keys instead.

Answer from Jack A. on Stack Overflow
🌐
The Coding Walrus
thecodingwalrus.com › js › javascript-for-loop-alternatives-2
Javascript For Loop Alternatives 2 | The Coding Walrus
July 28, 2020 - Find the best alternatives to the use of for loops in JavaScript. See use cases for Array methods map, reduce, and forEach and how the stack up against for loop alternatives.
🌐
Reddit
reddit.com › r/javascript › what is the performant alternative to nested for loops in javascript?
r/javascript on Reddit: What is the performant alternative to nested for loops in JavaScript?
July 20, 2017 -

Are nested for loops (in general - for loops) expensive in JavaScript? AFAIK prototype.map() is an alternative to for loop when it comes to code organisation but doesn't do well when memory, speed is taken into account. If for loops are expensive, what could be a performant alternative to nested for loop?

Discussions

javascript - alternative to for loop - Stack Overflow
That's because the arguments object ... variables change. If that's a concern, then the only option is to copy the arguments into a simple array with a for loop.... More on stackoverflow.com
🌐 stackoverflow.com
April 26, 2017
Alternative to for() loops?
I know this is JavaScript (lol), but personally I think using the for() loop to iterate through a list as we have been doing is both clunky and ugly, and I prefer a more "Pythonic" way of iterating through a list. ... Is there a better alternative to this approach that avoids the for loop entirely ... More on teamtreehouse.com
🌐 teamtreehouse.com
2
November 3, 2020
javascript - Alternative for "for of"-loop - Stack Overflow
This works, but it looks a little bit stupid. Firefox offers a for-of loop. Unfortunately it doesn't work in all browsers. More on stackoverflow.com
🌐 stackoverflow.com
loops - Alternatives to javascript function-based iteration (e.g. jQuery.each()) - Stack Overflow
I've been watching Google Tech Talks' Speed Up Your Javascript and in talking about loops, the speaker mentions to stay away from function-based iterations such as jQuery.each() (among others, at about 24:05 in the video). He briefly explains why to avoid them which makes sense, but admittedly I don't quite understand what an alternative ... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
3

I believe the answer you are looking for is linked in the accepted answer to the question you linked: http://www.jslint.com/help.html#forin

does not recommend use of the for in statement. Use Object.keys instead.

It goes on to explain why that is recommended.

The for in statement allows for looping through the names of all of the properties of an object. Unfortunately, it also loops through all of the properties that were inherited through the prototype chain. This has the bad side effect of serving up method functions when the interest is in data properties. If a program is written without awareness of this situation, then it can fail.

The body of every for in statement should be wrapped in an if statement that does filtering. It can select for a particular type or range of values, or it can exclude functions, or it can exclude properties from the prototype. For example,

for (name in object) {
    if (object.hasOwnProperty(name)) {
        ....
    }
}

Note that the above code will fail if the object contains a data property named hasOwnProperty. Use Object.keys instead.

2 of 2
1

I'm not quite sure what you mean by "but this seems like a longer way to do it." Since you get to remove the hasOwnProperty check, it's actually, in some ways, much cleaner.

Here's an edited version that lints:

/*jslint browser */
/*global Pub */

// on a truthy match returns true and breaks from loop
Pub.someKey = function (obj, func, con) {
    "use strict";
    if (Pub.isFunction(func)) {
        Object.keys(obj).forEach(function (key) {
            if (func.call(con, obj[key], key, obj)) {
                return true;
            }
        });
    }
    return false;
};
Pub.forSomeKey = Pub.someKey;

(I have a mild dislike of multiple return values, but edited this to use them to more closely match your original. And it gets rid of the "no breaks in forEach" issue I mentioned in the original.)


For more on why JSLint doesn't like for, see this Crockford video Setzer22 linked (in this question). It's interesting watching, and gives a little more context on why he doesn't like for... in specifically:

I've never liked for... in because it does that stupid thing where it dredges through the prototype and you get all the methods and stuff that you didn't want. So now we've got Object.keys, which returns an array of stuff and you don't get the dredge, and then pass that to forEach and its brothers, and it works great. So I don't use for... in any more either.

Bonus tip: Watching Crockford at .5 speed while transcribing is hilarious, at least for a few minutes.


Aside: Unfortunately, @Oriol's and Matt Burland's comments don't hold, as I imagine @YeWhoseNameCantBeAtted found out. You can add the for directive to use standard for (i=0;...), but you'll still get Expected 'Object.keys' and instead saw 'for in' in this use case.

🌐
The Coding Walrus
thecodingwalrus.com › js › javascript-for-loop-alternatives-1
Javascript For Loop Alternatives 1 | The Coding Walrus
July 16, 2020 - Find the best alternatives to the use of for loops in JavaScript. See use cases for Array methods find, fiter, and slice and how they stack against for loop alternatives.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop.
🌐
YouTube
youtube.com › coding with estefania
6 Powerful Ways of Replacing for Loops in JavaScript - YouTube
Are you looking for cleaner alternatives to the traditional JavaScript for loops? If you are, in this video I will introduce you to 6 powerful methods that y...
Published   February 14, 2024
Views   586
🌐
JavaScript in Plain English
javascript.plainenglish.io › 7-javascript-alternatives-to-the-traditional-for-loop-edcb71fe0b32
7 JavaScript Alternatives to the Traditional ‘For’ Loop | by Nanda Vikas Konduru | JavaScript in Plain English
November 3, 2023 - Let's explore some of these alternatives. ... The for...of loop was introduced with ES6 and allows for a cleaner syntax to iterate over iterable objects. ... More concise and readable than the traditional for loop, especially when the index isn't required. Works with any iterable, not just arrays (e.g., strings, maps, sets). JavaScript arrays come equipped with a suite of built-in methods that offer more than just iteration.
Find elsewhere
🌐
C# Corner
c-sharpcorner.com › article › javascript-array-foreach-method-and-its-alternatives
JavaScript Array forEach Method And Its Alternatives
June 26, 2023 - The some() function will test all elements of an array, but only one element must pass the test, thus making it a good candidate as an alternative to forEach. Once it passes a certain condition, it will return out of the Loop.
🌐
Team Treehouse
teamtreehouse.com › community › alternative-to-for-loops
Alternative to for() loops? (Example) | Treehouse Community
November 3, 2020 - But if you use the querySelectorAll method, you get back a NodeList instead, and you can use .forEach() directly on a NodeList without the spread operator. Full Stack JavaScript Techdegree Student 14,652 Points
🌐
Bitstack
blog.bitsrc.io › please-use-map-instead-of-for-loops-5a2f54f088c8
Use map() instead of for() loops | Bits and Pieces
March 4, 2025 - Now, I am sure you will recognize the listOfPeople[i].id piece of the code here from the for() loop example. To explain this further, the .map() function is “looping” over each item in the array and assigning the item (i.e. person in the second and third examples) and the current index (i.e.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › alternatives-of-for-loops-and-if-else-blocks-in-reactjs
Alternatives of for loops and if-else blocks in ReactJS - GeeksforGeeks
July 23, 2025 - JavaScript · Web Technology · Sign In ▲ · Open In App · Last Updated : 23 Jul, 2025 · Comments · Improve · Suggest changes · Like Article · Like · Report · Implementing loops in React generally gives the error stating Unexpected token and the same error shows up if you use if conditions as well. There we have alternatives to be used in JSX in react. Syntax for Loop in React: function App() { return ( <div> {for(let i =0 ; i< 5 ; i++) </Component>} </div> } Explanation: Browser doesn't understand react.js so webpack such as Babel converts React.js into JavaScript at compilation.
🌐
Medium
medium.com › @argeegabrielii › foreach-method-and-its-alternatives-f7078b2a05c4
forEach Method and It’s Alternatives | by W1lz0R | Medium
March 22, 2024 - It's a concise way to perform a task on every item in an array without needing a traditional loop. ... // Javascript let items = ["keyboard", "mouse", "laptop", "monitor"]; items.forEach(myFunction); const myFunction = (item) => { console.log(item); }
🌐
Krasimirtsonev
krasimirtsonev.com › blog › article › An-alternative-JavaScript-for-loop-syntax
An alternative JavaScript for loop syntax
November 8, 2013 - The loop will stop on the third element, because user will be false. We could of course solve this by checking the type of the variable: for(var i=0; typeof (user=users[i]) !== "undefined"; i++) { // ...
🌐
Jrsinclair
jrsinclair.com › articles › 2017 › javascript-without-loops
JavaScript Without Loops - James Sinclair
We’ve been talking about writing less complex JavaScript. We do this by choosing the right abstraction to solve a problem. But how do you know which abstraction to use? So far, we haven’t looked at any concrete examples of how to do this. In this article we look at how to deal with JavaScript arrays, without using any loops.
🌐
Medium
medium.com › @jamesmarks › how-to-almost-never-use-another-for-or-while-loop-in-javascript-c29657191dc2
How to (almost) Never Use Another `for` or `while` Loop in Javascript | by James Marks | Medium
March 29, 2023 - The Javacript Array prototype has several powerful non-mutative alternatives that you can use to loop over arrays and other array-like values.
🌐
HackerNoon
hackernoon.com › rethinking-javascript-death-of-the-for-loop-c431564c84a8
Rethinking JavaScript: Death of the For Loop | HackerNoon
January 11, 2017 - JavaScript’s for loop has served us well, but it is now obsolete and should be retired in favor of newer functional programming techniques.
🌐
Medium
dltlabs.medium.com › for-loops-are-old-news-8d67172559e2
'FOR' loops are old news. We all have been using ... - DLT Labs
December 14, 2021 - We all have been using ‘for’ loop since we started learning how to code isn’t it? But, what if I told you there was an alternative to the venerable ‘for’ loop? No matter which programming language it is — be it is Java, C, Python, JavaScript, etc.