Lodash _.forIn() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.forIn(object, iteratee) visits own and inherited enumerable string-keyed properties.
  • The iteratee signature: (value, key, object).
  • How to stop iteration early by returning false.
  • When to choose _.forIn, _.forOwn, or native for...in.

Prerequisites

Know the difference between an object’s own properties and properties inherited through its prototype chain.

  • Enumerable properties: only keys visible to property iteration are visited; non-enumerable properties are ignored.
  • Prototype chain: inherited enumerable keys are included, which is the main difference from _.forOwn.

Overview

_.forIn invokes an iteratee for every own and inherited enumerable string key on an object. It returns the original object, so it is best for side effects such as logging, validation, collecting keys, or controlled mutation.

Own + inherited

Use it when prototype-provided enumerable properties are intentionally part of the surface.

Early exit

Return false from the iteratee to stop looping before every key is visited.

Not a mapper

The return value is the original object. Build a result yourself if you need transformed output.

Syntax

javascript
_.forIn(object, [iteratee=_.identity])
  • object: object to iterate over.
  • iteratee: function invoked with (value, key, object).
  • Early exit: return false to stop iteration.
  • Returns: the original object.
1

Iterate over key-value pairs

For a plain object, _.forIn feels similar to looping over Object.keys(), but the iteratee gives you value, key, and object directly.

javascript
import forIn from "lodash/forIn";

const user = {
  name: "John",
  age: 30,
  city: "New York"
};

const lines = [];
forIn(user, (value, key) => {
  lines.push(`${key}: ${value}`);
});

// lines -> ["name: John", "age: 30", "city: New York"]
Try it Yourself
2

Inherited enumerable keys are included

This is the defining behavior. _.forIn visits enumerable properties from the prototype chain; _.forOwn does not.

javascript
import forIn from "lodash/forIn";
import forOwn from "lodash/forOwn";

function Profile(name) {
  this.name = name;
}

Profile.prototype.role = "member";

const profile = new Profile("Ava");

const forInKeys = [];
forIn(profile, (value, key) => forInKeys.push(key));

const forOwnKeys = [];
forOwn(profile, (value, key) => forOwnKeys.push(key));

// forInKeys  -> ["name", "role"]
// forOwnKeys -> ["name"]
Try it Yourself
3

Stop early by returning false

Unlike forEach-style loops where developers often forget the break pattern, Lodash iteration helpers stop when the iteratee explicitly returns false.

javascript
import forIn from "lodash/forIn";

const config = {
  host: "localhost",
  port: 3000,
  token: "",
  retries: 3
};

let firstEmptyKey;
forIn(config, (value, key) => {
  if (value === "") {
    firstEmptyKey = key;
    return false; // stop here
  }
});

// firstEmptyKey -> "token"
Try it Yourself

📋 _.forIn vs _.forOwn vs native for...in

Topic_.forIn_.forOwnfor...in
Own keysYesYesYes
Inherited enumerable keysYesNoYes
Symbol keysNoNoNo
Callback args(value, key, object)(value, key, object)Key only
Early exitReturn falseReturn falsebreak

Most app code wants _.forOwn. Reach for _.forIn when inherited enumerable keys are intentionally meaningful.

Pitfalls to avoid

Inherited

Prototype keys may surprise you

_.forIn includes inherited enumerable keys. Use _.forOwn when you only want fields stored directly on the object.

Return

It returns the original object

Returning a transformed value from the iteratee does not build an output object. Use _.mapValues or _.transform for transformations.

Symbols

Symbol keys are skipped

If your object uses symbols, use native reflection helpers such as Object.getOwnPropertySymbols() alongside regular key iteration.

Order

Do not encode business rules in object order

Use arrays when order is part of the data model. Object key order can be predictable in many cases, but it is the wrong abstraction for ordered workflows.

❓ FAQ

Own and inherited enumerable string-keyed properties. Symbol keys are skipped, and non-enumerable properties are skipped.
The iteratee receives (value, key, object). The original object is passed as the third argument.
Yes. Return false explicitly from the iteratee to stop iteration early.
The original object. It is an iteration helper, not a mapper. If you need a new object, build one yourself or use helpers like _.mapValues or _.transform.
_.forIn visits inherited enumerable keys too. _.forOwn visits only the object's own enumerable keys.
Do not rely on it for business logic. It follows JavaScript property iteration behavior, but object key ordering rules are not a replacement for an ordered array when order matters.

Summary

Did you know?

_.forIn is closer to JavaScript's native for...in loop than to Object.keys(): it visits both own and inherited enumerable string keys. Use _.forOwn when inherited keys should be ignored.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful