Lodash _.forOwn() method

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

What you’ll learn

  • How _.forOwn(object, iteratee) visits an object’s own enumerable string keys only.
  • The iteratee signature (value, key, object) and the early-exit pattern (return false).
  • Why _.forOwn is the modern replacement for the old for...in + hasOwnProperty dance.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Familiarity with own vs inherited properties (or skim _.forIn() for the difference).

  • Own properties: only keys assigned directly to the object are visited—same set as Object.keys().
  • Iteratee callbacks: remember the (value, key, object) argument order—same as _.forIn.
  • Side effect mindset: the return value is the original object. Build a fresh result yourself if needed.

Overview

_.forOwn invokes the iteratee for every own enumerable string key on the object, ignoring anything on the prototype chain. It returns the original object, which makes it ideal for logging, validation, or in-place mutation of plain data objects.

Own keys only

Inherited keys are skipped automatically—no hasOwnProperty boilerplate.

Early exit

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

Not a mapper

Returns the original object. Use _.mapValues / _.transform if you need transformed output.

Syntax

javascript
_.forOwn(object, [iteratee=_.identity])
  • object: object whose own enumerable string keys are iterated.
  • iteratee: function invoked with (value, key, object).
  • Early exit: return false to stop iteration.
  • Returns: the original object.
1

Iterate over an object’s own properties

For a plain object, _.forOwn visits each key once and gives you the value and key directly—cleaner than Object.keys(obj).forEach(...).

javascript
import forOwn from "lodash/forOwn";

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

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

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

Inherited keys are skipped

This is the defining behavior. _.forOwn ignores enumerable keys on the prototype chain; _.forIn visits them.

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

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

Profile.prototype.role = "member";

const profile = new Profile("Ava");

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

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

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

Mutate in place: drop empty values

Because the third iteratee argument is the original object, you can use _.forOwn to mutate or clean it in place.

javascript
import forOwn from "lodash/forOwn";

const personInfo = {
  name: "Sam",
  age: 22,
  email: "sam@example.com",
  address: ""
};

forOwn(personInfo, (value, key, obj) => {
  if (value === "") {
    delete obj[key];
  }
});

// personInfo -> { name: "Sam", age: 22, email: "sam@example.com" }
Try it Yourself

📋 _.forOwn vs _.forIn vs Object.entries

Topic_.forOwn_.forInObject.entries(...).forEach
Own keysYesYesYes
Inherited enumerable keysNoYesNo
Symbol keysNoNoNo
Callback args(value, key, object)(value, key, object)([key, value], index, entries)
Early exitReturn falseReturn falseThrow or use for...of + break
AllocatesNoNoYes (intermediate array)

Most app code wants _.forOwn: it’s the safe default for plain data objects, with zero intermediate allocation.

Pitfalls to avoid

Inherited

Picked the wrong helper

If you actually need prototype-provided enumerable keys (mixins, framework-defined behavior), use _.forIn. _.forOwn will silently skip them.

Return

Returning a new value does nothing

The iteratee’s return value is only checked for false (early exit). Use _.mapValues or build a result object yourself for transformations.

Symbols

Symbol keys are skipped

Use Object.getOwnPropertySymbols() alongside _.forOwn if your object has symbol keys.

Mutation

Adding keys mid-iteration

Deleting the current key during iteration is safe. Adding new keys is implementation-defined—collect them and apply after the loop.

❓ FAQ

Only the object's own enumerable string-keyed properties. Inherited prototype keys and symbol keys are skipped.
(value, key, object). Value comes first, then the key, then the original object reference.
Yes. Return false explicitly from the iteratee to stop iteration before every key is visited.
The original object. It is an iteration helper for side effects, not a mapper. Use _.mapValues, _.mapKeys, or _.transform when you need a new object.
_.forOwn iterates own enumerable string keys only. _.forIn additionally walks inherited enumerable string keys from the prototype chain.
Usually yes. Native for...in walks the prototype chain, so you typically need to guard each iteration with hasOwnProperty. _.forOwn handles that for you.
Yes; deleting the current key during iteration is safe in Lodash's implementation. Adding new keys while iterating is undefined territory—collect changes and apply them after the loop.

Summary

Did you know?

_.forOwn is the modern stand-in for for (key in obj) + hasOwnProperty. It skips inherited prototype keys for you and gives the iteratee value, key, and the original object as separate arguments.

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