Lodash _.forIn() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Iterates over an object’s own and inherited enumerable string keys, calling the iteratee for each. Closer to native for…in than to Object.keys. Since Lodash 0.3.0.

Pass the object and path or keys shown below. Check whether the helper mutates or returns a new object.

Syntax

Call _.forIn. Start with this line.

Lodash
_.forIn(object, [iteratee=_.identity])
CallWhat it does
_.forIn(object, [iteratee=_.identity])Primary signature.
_.forIn(object)Work with an object’s keys, values, or paths.

Prefer import forIn from "lodash/forIn" when you want a smaller bundle.

Library declarations
Lodash
_.forIn(object, [iteratee=_.identity])

Parameters

ParameterDescription
objectRequired. Object to iterate (returned unchanged)
iterateeOptional. Defaults to _.identity

Return Value

Return ValueDescription
*The original object

Lodash Examples

Example 1 — Official: Own + Inherited

Prototype enumerable keys are visited too.

Lodash
function Foo() {
  this.a = 1;
  this.b = 2;
}
Foo.prototype.c = 3;

var keys = [];
_.forIn(new Foo(), function (value, key) {
  keys.push(key);
});
// → ["a", "b", "c"]
Try It Yourself

How It Works

  1. Instance owns a and b.
  2. Prototype adds enumerable c.
  3. forIn visits all three string keys.
  4. Iteratee receives (value, key, object) each time.

Example 2 — vs _.forOwn

forOwn skips the prototype; forIn includes it.

Lodash
var obj = new Foo();
var withIn = [];
var withOwn = [];

_.forIn(obj, function (v, k) { withIn.push(k); });
_.forOwn(obj, function (v, k) { withOwn.push(k); });

// withIn  → ["a", "b", "c"]
// withOwn → ["a", "b"]
Try It Yourself

How It Works

  1. Same instance for both calls.
  2. forIn walks the prototype chain → includes c.
  3. forOwn stops at own keys → no c.
  4. Pick forOwn for “instance fields only.”

Example 3 — Early Exit with false

Return false to stop as soon as you find what you need.

Lodash
var seen = [];
_.forIn({ a: 1, b: 2, c: 3 }, function (value, key) {
  seen.push(key);
  if (key === "b") return false;
});
// seen → ["a", "b"]
Try It Yourself

How It Works

  1. a is visited; iteratee keeps going.
  2. b matches → return false.
  3. c is never visited.
  4. Tip. Only an explicit false stops; other falsy returns do not.

Example 4 — Collect Keys (Returns Object)

_.forIn returns the original object — handy for chaining side effects.

Lodash
var source = { x: 10, y: 20 };
var pairs = [];

var same = _.forIn(source, function (value, key) {
  pairs.push(key + "=" + value);
});

same === source; // true
// pairs → ["x=10", "y=20"]
Try It Yourself

How It Works

  1. Iteratee builds a side-effect list of pairs.
  2. Return value is still source, not a new array.
  3. Not a mapper — use _.mapValues / _.transform to build results.
  4. Pattern. Good for logging, counting, or filling an external array.

Example 5 — vs _.forInRight

Same keys; opposite walk order.

Lodash
var left = [];
var right = [];
var data = { a: 1, b: 2, c: 3 };

_.forIn(data, function (v, k) { left.push(k); });
_.forInRight(data, function (v, k) { right.push(k); });

// left  → ["a", "b", "c"]
// right → ["c", "b", "a"]
Try It Yourself

How It Works

  1. forIn walks forward (typical insertion order).
  2. forInRight walks the same enumerable keys backward.
  3. Same set of keys — only order changes.
  4. Pick forInRight when the last property should run first.

Browser & Runtime Support

_.forIn is a Lodash Object helper (since 0.3.0). It runs anywhere Lodash 4.x runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 0.3.0+

_.forIn()

Available in [email protected] full builds and the lodash/forIn modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.forIn Lodash 0.3.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import forIn from "lodash/forIn". Use forOwn when inherited keys should be ignored.

Frequently Asked Questions

Own and inherited enumerable string-keyed properties. Symbol keys and non-enumerable properties are skipped.
(value, key, object). Value comes first, then the key, then the original object.
Yes. Return false explicitly from the iteratee to exit iteration early.
The original object. It is an iteration helper, not a mapper.
_.forIn visits inherited enumerable keys too. _.forOwn visits only own enumerable keys.
Same keys; forInRight walks them in reverse order.

Next: _.forInRight()

Iterate own and inherited enumerable keys in reverse order.

_.forInRight() tutorial →

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.

11 people found this page helpful