Lodash _.before() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Creates a function that invokes func while it is called less than n times. Later calls return the last func result. Part of Lodash 4.x Function (since 3.0.0).

Pass the required arguments shown below. Most helpers return a new function you call later.

Syntax

Call _.before. Start with this line.

Lodash
_.before(n, func)
CallWhat it does
_.before(n, func)Primary signature.
_.before(func)Wrap or transform a function.

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

Library declarations
Lodash
_.before(n, func)

Parameters

ParameterDescription
nRequired. Call count where func stops (coerced with toInteger)
funcRequired. Function to restrict; throws if not a function

Return Value

Return ValueDescription
*New restricted function

Lodash Examples

Example 1 — Basic before(3)

Func runs on the first two calls only.

Lodash
var count = 0;
var limited = _.before(3, function () {
  count += 1;
  return count;
});

[limited(), limited(), limited(), limited()];
// → [1, 2, 2, 2]
Try It Yourself

How It Works

  1. Wrap. _.before(3, fn) allows at most 2 real runs.
  2. Call 1–2. Counter passes --n > 0 → fn updates count.
  3. Call 3+. Guard fails → last result 2 is returned again.
  4. Use. Cap how often a side effect may fire.

Example 2 — Official-Style Contact Cap

Allow up to 4 adds with before(5) — same idea as the docs.

Lodash
var contacts = [];
var addContact = _.before(5, function (name) {
  contacts.push(name);
  return contacts.length;
});

["Ada", "Lin", "Sam", "Jo", "Pat", "Kim"].forEach(function (name) {
  addContact(name);
});

contacts;
// → ["Ada", "Lin", "Sam", "Jo"]
Try It Yourself

How It Works

  1. Docs. _.before(5, …) allows 4 successful adds.
  2. Push. First four names are stored.
  3. Stop. Fifth and sixth calls skip func — no more pushes.
  4. Handlers. Same pattern works for click / submit listeners.

Example 3 — Freezes the Last Result

After the limit, the wrapper keeps returning the last value.

Lodash
var greet = _.before(3, function (name) {
  return "Hi " + name;
});

[greet("Ada"), greet("Lin"), greet("Sam")];
// → ["Hi Ada", "Hi Lin", "Hi Lin"]
Try It Yourself

How It Works

  1. Call 1–2. func builds fresh strings.
  2. Call 3. Limit hit — "Sam" is ignored.
  3. Replay. Last result "Hi Lin" is returned again.
  4. GC. Lodash clears the internal func reference after freezing.

Example 4 — before(1) Never Runs

With n = 1, the guard never lets func execute.

Lodash
var ran = false;
var noop = _.before(1, function () {
  ran = true;
  return "x";
});

[noop(), ran];
// → [undefined, false]
Try It Yourself

How It Works

  1. First call. --n makes n = 0.
  2. Guard. 0 > 0 is false → func skipped.
  3. Result. Stays undefined; side effects never fire.
  4. Want once? Use _.before(2, fn) or _.once(fn).

Example 5 — before vs after

Same n, opposite timing windows.

Lodash
var early = [];
var late = [];

var b = _.before(3, function (x) { early.push(x); });
var a = _.after(3, function (x) { late.push(x); });

[1, 2, 3, 4].forEach(function (x) {
  b(x);
  a(x);
});

early; // → [1, 2]
late;  // → [3, 4]
Try It Yourself

How It Works

  1. before(3). Runs on calls 1–2, then freezes.
  2. after(3). Skips 1–2; runs on 3 and 4.
  3. Pick. Caps / “first few only” → before. Barriers → after.
  4. Once. For a single fire, prefer _.once.

Browser & Runtime Support

_.before is a Lodash 4.x Function helper (since 3.0.0). It needs no special browser APIs beyond loading Lodash.

✓ Stable · Lodash 3.0.0+

_.before()

Available in [email protected] full builds and the lodash/before modular import. Works in evergreen browsers and Node / Bun / Deno 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
_.before Lodash 4.x+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import before from "lodash/before" for smaller builds.

Frequently Asked Questions

_.before(n, func) returns a function that invokes func while the call count is less than n (at most n−1 times). Later calls return the last result without calling func again.
before runs early (first n−1 calls) then freezes. after waits until call n, then runs func on that call and every later call.
At most n − 1 times. _.before(5, fn) runs fn up to 4 times — matching the official click-cap example.
func never runs. On the first call the counter drops to 0 before the --n > 0 guard can pass, so the result stays undefined.
_.once(func) is like running at most once. _.before(2, func) also invokes func on the first call and freezes afterward.
Use import before from "lodash/before" or require("lodash/before") for tree-shaking. Full build: import _ from "lodash" then _.before.

Next: _.bind()

Fix this and prepend partial arguments with Lodash bind (and placeholders).

_.bind() 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.

10 people found this page helpful