Surprising behavior of for...in loop

Full Stack Developer | Freelancer | JavaScript | React | Nodejs.
Loves sharing knowledge through technical articles.
Dev.to: https://dev.to/myogeshchavan97
https://linktr.ee/myogeshchavan97
Search for a command to run...

Full Stack Developer | Freelancer | JavaScript | React | Nodejs.
Loves sharing knowledge through technical articles.
Dev.to: https://dev.to/myogeshchavan97
https://linktr.ee/myogeshchavan97
No comments yet. Be the first to comment.
In this tutorial, you will build a Task Management App using Next.js 16 and Prisma 7 from scratch. By creating this app, you will learn: How to set up Prisma 7 with the new Rust-free architecture How to configure a database with Prisma's driver ada...

If you're a React/Next.js developer who diligently updated your packages last week after the critical React2Shell vulnerability (CVE-2025-55182), I have some frustrating news: you need to update again. Security researchers, while probing the patches ...

Is your React app dragging its feet, but you can’t pinpoint the cause? You might think you’re following the best practices, but some common development patterns are actually degrading your app’s performance behind the scenes. In this article, we’ll u...

How to Use Separate SSH Keys for Company and Personal GitHub Accounts Managing multiple GitHub accounts is a common challenge for developers who juggle both personal and professional projects. It becomes even more complex when you want to use differe...

Have you ever built a React app only to realize that users from different countries can't use it effectively because it only supports English? In this tutorial, you'll learn how to build truly global React application using internationalization (i18n...

In this article, we will see how to use for...in loops to avoid unpredictable bugs.
Take a look at the below code:
const user = { name: 'David', age: 30 };
for (key in user) {
console.log(user[key]);
}
Can you predict the output of the above code?
The output will be:
David
30
which is as expected.
Now take a look at the below code:
const user = { name: 'David', age: 30 };
Object.prototype.color = 'red';
Object.prototype.display = function() {};
for (key in user) {
console.log(user[key]);
}
What will be the output now?

So it prints the color property and display function also, which is obviously not what we expected.
So if somewhere in your code, you or someone has added some properties or methods to the Object prototype, they will be displayed when using a for...in loop.
This means the for...in loop will display its own properties and also its prototype properties.
We can fix this using the hasOwnProperty method.
It has the following syntax:
obj.hasOwnProperty(prop)
The method returns true if the prop provided is the obj’s own declared property.
const user = { name: 'David', age: 30 };
Object.prototype.color = 'red';
Object.prototype.display = function() {};
for (key in user) {
if (user.hasOwnProperty(key)) {
console.log(user[key]);
}
}
Now, the output will come as expected.

There is another way to fix this issue using the Object.keys method and using the for...of loop.
It has the following syntax:
Object.keys(obj)
This method returns an array of a given object’s own enumerable property names.
const user = { name: 'David', age: 30 };
Object.prototype.color = 'red';
Object.prototype.display = function() {};
for (key of Object.keys(user)) {
console.log(user[key]);
}

So you might need to take note of these things when using the for...in loop.
Check out my recently published Mastering Redux course.
In this course, you will build 3 apps along with a food ordering app and you'll learn:
and then finally we'll build a complete food ordering app from scratch with stripe integration for accepting payments and deploy it to the production.
Want to stay up to date with regular content regarding JavaScript, React, Node.js? Follow me on LinkedIn.