Underscore.js _.once() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

Creates a version of a function that can only be called one time. Repeated calls have no further effect and return the value from the original call (official Underscore docs). Stable Function helper — not deprecated.

Pass the required arguments shown below. Underscore function helpers usually return a new function — check timing and call-count behavior carefully.

Syntax

Call _.once. Start with this line.

Underscore
_.once(function)
CallWhat it does
_.once(function)Primary signature.
_.once(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.once(function)

Parameters

ParameterDescription
functionRequired. Function to wrap

Return Value

Return ValueDescription
*Wrapper that runs once

Underscore.js Examples

Example 1 — Run Only Once

Three wrapper calls — the original body runs a single time.

Underscore
var count = 0;
var greet = _.once(function () {
  count++;
  return "hello";
});

greet();
greet();
greet();
// count === 1
Try It Yourself

How It Works

  1. Wrap. _.once returns a guarded function.
  2. First call. Runs the body; count becomes 1.
  3. Next calls. Body skipped entirely.
  4. No flag. You don’t manage an “already done” boolean yourself.

Example 2 — Reuse the First Result

Later calls return the same value from the first run.

Underscore
var makeId = _.once(function () {
  return "id-" + Math.floor(Math.random() * 1000);
});

var a = makeId();
var b = makeId();
// a === b  → true
Try It Yourself

How It Works

  1. First call. Random id is computed and stored.
  2. Second call. Random code does not run again.
  3. Same value. a and b match.
  4. Stable. Good for a singleton token or client id.

Example 3 — One-Time Setup

Docs-style init — call setup from many places safely.

Underscore
var log = [];
var initialize = _.once(function () {
  log.push("created");
  return "app";
});

initialize();
initialize();
// log → ["created"]  (only once)
Try It Yourself

How It Works

  1. Wrap create. Like _.once(createApplication) in the docs.
  2. First call. Pushes "created" and returns "app".
  3. Second call. No second push.
  4. Safe re-entry. Call from modules without double-init.

Example 4 — Later Arguments Ignored

Only the first call’s arguments are used.

Underscore
var greet = _.once(function (name) {
  return "hi " + name;
});

greet("Ada");    // → "hi Ada"
greet("Linus");  // → "hi Ada"  (still first result)
Try It Yourself

How It Works

  1. First args win. "Ada" is baked into the result.
  2. Second call. "Linus" never reaches the body.
  3. Not memoize. Different keys do not trigger new runs.
  4. Need per-arg cache? Use _.memoize.

Browser & Runtime Support

_.once ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.once()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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
_.once Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]). Native alternative: a closure with a called flag that stores the first return value.

Frequently Asked Questions

_.once(function) returns a version of the function that can only run one time. The first call executes the original and remembers its result; later calls return that same result without running again.
The value from the first call. Side effects in the original function happen only once.
_.once runs once total, ignoring later arguments. _.memoize can run many times — once per unique cache key.
_.once(fn) is the same idea as _.before(2, fn): allow the first call, then stop.
One-time initialization, setup, or handlers that must not fire twice (create a connection, bootstrap an app).
No. It is a stable Underscore Function helper.

Next: _.after()

Create a function that only runs after it has been called n times.

_.after() →

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