Lodash _.takeRightWhile() method

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

What you’ll learn

  • How _.takeRightWhile(array, predicate) isolates a predicate-stable suffix.
  • Why only the contiguous tail qualifies—earlier matches stay behind.
  • How this differs from fixed counts (_.takeRight()) and global filters.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Skim _.dropRightWhile() for the complementary operation—it removes the suffix takeRightWhile would keep.

  • You can read arrow predicates and Lodash iteratee shorthand ("prop", paths).
  • You can open Try-it labs or run snippets locally.

Overview

_.takeRightWhile shines when trailing noise shares a pattern—parity checks, sentinel flags, nullable padding—but you cannot hard-code how many cells participate.

End-first scan

The predicate walks inward until it falters; everything after that pivot stays excluded.

Stable ordering

Winners remain sorted left-to-right relative to each other even though evaluation reversed direction.

Fresh array

Source buffers remain untouched for auditing or alternate predicates.

Syntax

javascript
_.takeRightWhile(array, [predicate=_.identity])
  • array: collection or array-like input.
  • predicate: invoked per element as (value, index, array); trailing cells join the result while truthy. Supports iteratee shorthand such as property strings.
  • Returns: new array holding only that contiguous trailing suffix.
1

Trailing run of even numbers

Scanning from the right: 14, 12, and 10 pass; 5 stops the suffix.

javascript
import takeRightWhile from "lodash/takeRightWhile";

takeRightWhile([5, 10, 12, 14], (n) => n % 2 === 0);
// → [10, 12, 14]
Try it Yourself
2

Immediate miss

When the final element already fails the predicate, nothing is collected.

javascript
import takeRightWhile from "lodash/takeRightWhile";

takeRightWhile([1, 3, 5], (n) => n % 2 === 0);
// → []
Try it Yourself
3

Property shorthand

String iteratees test truthiness of a field—here only objects still marked closed survive at the tail.

javascript
import takeRightWhile from "lodash/takeRightWhile";

takeRightWhile(
  [
    { id: 1, closed: false },
    { id: 2, closed: true },
    { id: 3, closed: true }
  ],
  "closed"
);
// → [{ id: 2, closed: true }, { id: 3, closed: true }]
Try it Yourself

📋 takeRightWhile vs takeRight, dropRightWhile, filter

APISelection ruleContrast
_.takeRightWhile(arr, pred)Longest truthy suffixReturns only that tail segment
_.takeRight(arr, n)Fixed countNo predicate—length known up front
_.dropRightWhile(arr, pred)Removes truthy suffixKeeps complementary prefix instead
_.filter(arr, pred)Every matching elementNot limited to one contiguous edge

Pitfalls to avoid

Suffix

Not global

Interior matches never surface—only the block touching the final index qualifies.

Pure

Predicate hygiene

Reverse evaluation order makes sneaky mutable state harder to reason about—keep predicates pure.

Deep

Shallow copies

Objects inside the suffix remain referenced—clone deeply when isolation matters.

❓ FAQ

No. Lodash returns a new array containing the matched trailing slice only.
It evaluates from the last index backward but preserves ascending element order in the returned suffix.
You receive an empty array—the scan stops immediately.
The entire collection copies shallowly into the result.
Use import takeRightWhile from "lodash/takeRightWhile" or require("lodash/takeRightWhile") for tree-shaken bundles.

Summary

Did you know?

The iteratee still receives (value, index, array) with ordinary left-to-right index values—only the visit order starts from the end. Pair mentally with _.dropRightWhile(): one keeps the tail you trim with the other.

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