Lodash _.forInRight() method

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

What you’ll learn

  • How _.forInRight(object, iteratee) walks own and inherited enumerable string keys from last to first.
  • The iteratee signature (value, key, object) and the early-exit pattern (return false).
  • When reverse iteration genuinely matters (cleanup, last-wins overrides) versus when it’s a smell that you should use an array instead.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.forIn() first; _.forInRight only changes the scan direction.

  • Own vs inherited keys: inherited enumerable keys are included—the same surface as _.forIn.
  • Iteration order: reverse of _.forIn’s order. Symbol keys and non-enumerable properties are skipped.
  • Mutation discipline: deleting keys mid-iteration is safer in reverse order, since it doesn’t shift positions that haven’t been visited yet.

Overview

_.forInRight invokes the iteratee for each own and inherited enumerable string key—but from the last key to the first. The original object is returned, so this is a side-effect helper for logging, cleanup, or last-wins overrides.

Reverse scan

Same key coverage as _.forIn, walked from last to first.

Own + inherited

Enumerable prototype keys are visited too. Use _.forOwnRight if you only care about own keys.

Early exit

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

Syntax

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

Iterate in reverse key order

For a plain object, _.forInRight visits the keys from the last-inserted one back to the first.

javascript
import forInRight from "lodash/forInRight";

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

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

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

Inherited enumerable keys are included

Just like _.forIn, the prototype’s enumerable keys show up. The difference from _.forOwnRight is exactly this inherited coverage.

javascript
import forInRight from "lodash/forInRight";
import forOwnRight from "lodash/forOwnRight";

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

Animal.prototype.sound = "Unknown";

const cat = new Animal("Whiskers");

const forInRightKeys = [];
forInRight(cat, (value, key) => forInRightKeys.push(key));

const forOwnRightKeys = [];
forOwnRight(cat, (value, key) => forOwnRightKeys.push(key));

// forInRightKeys  -> ["sound", "name"]
// forOwnRightKeys -> ["name"]
Try it Yourself
3

Reverse cleanup with early exit

Reverse iteration is a good fit when later resources depend on earlier ones—the most recently allocated thing is released first. Returning false stops the loop the moment a problem is detected.

javascript
import forInRight from "lodash/forInRight";

const resources = {
  database: { close: () => "db closed" },
  cache:    { close: () => "cache closed" },
  logger:   null
};

const log = [];
forInRight(resources, (resource, key) => {
  if (!resource) {
    log.push(`skip ${key}`);
    return false; // stop here
  }
  log.push(resource.close());
});

// log -> ["skip logger"]
//   the loop bailed out before touching cache or database
//   because logger was scanned first in reverse order.
Try it Yourself

📋 _.forInRight vs _.forIn vs _.forOwnRight

Topic_.forInRight_.forIn_.forOwnRight
Own keysYesYesYes
Inherited enumerable keysYesYesNo
Iteration orderReverseForwardReverse
Symbol keysNoNoNo
Callback args(value, key, object)(value, key, object)(value, key, object)
Early exitReturn falseReturn falseReturn false

Pick _.forInRight when both inherited keys and reverse direction matter. If only direction matters and you only care about own keys, _.forOwnRight is the safer default.

Pitfalls to avoid

Order

“Reverse” refers to key order, not chronological time

If timestamps or insertion semantics matter, use an array of entries and reverse that explicitly instead of leaning on object key order.

Inherited

Prototype keys may surprise you

Inherited enumerable keys are included. Use _.forOwnRight if 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.

Mutation

Adding keys mid-iteration is undefined territory

Deleting keys during a reverse walk is generally safe, but inserting new keys can shift positions or cause skipped/double visits. Plan structural changes outside the loop.

❓ FAQ

Own and inherited enumerable string-keyed properties, just like _.forIn. The only difference is iteration order: it walks the keys from last to first.
The iteratee receives (value, key, object). The third argument is the original object you passed in.
Yes. Return false explicitly from the iteratee to break out before every key is visited.
The original object. It is an iteration helper, not a mapper. Build a new object yourself, or use _.mapValues / _.transform when you need transformed output.
Same key coverage (own + inherited enumerable string keys), opposite iteration direction. _.forIn walks first-to-last; _.forInRight walks last-to-first.
_.forInRight visits inherited enumerable keys too. _.forOwnRight visits only the object's own enumerable keys, also in reverse order.
It is the reverse of whatever order _.forIn would use. Object property ordering is predictable in modern engines for non-integer keys, but it is still safer to use an array when ordering is part of the data model.

Summary

Did you know?

_.forInRight reverses only the iteration order—not the shape of the data. The object itself is not mutated unless your iteratee writes back into it through the third argument.

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