Lodash _.times() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Util

Definition

Invokes an iteratee n times and returns an array of the results. The iteratee receives one argument: the zero-based index. Part of Lodash Util since 0.1.0 (stable — not deprecated).

Note: Default iteratee is _.identity, so _.times(3) yields [0, 1, 2].

Syntax

Call _.times. Start with this line.

Lodash
_.times(n, [iteratee=_.identity])
CallWhat it does
_.times(n, [iteratee=_.identity])Primary signature.
_.times(...)Call the util helper.

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

Library declarations
Lodash
_.times(n, [iteratee=_.identity])

Parameters

ParameterDescription
nRequired. How many times to invoke the iteratee
iterateeOptional. Called with (index); default _.identity

Return Value

Return ValueDescription
*Array of each invocation’s return value

Lodash Examples

Example 1 — Official: String Iteratee

Convert each index to a string — the docs’ canonical demo.

Lodash
_.times(3, String);
// → ['0', '1', '2']
Try It Yourself

How It Works

  1. Count. n = 3 means three calls.
  2. Index. Lodash passes 0, then 1, then 2.
  3. Convert. String(index) turns each number into text.
  4. Collect. Results land in a new array in order.

Example 2 — Map Each Index

Return a computed value from the index — double each slot.

Lodash
_.times(3, function (i) {
  return i * 2;
});
// → [0, 2, 4]
Try It Yourself

How It Works

  1. Iteratee. Receives i as the current index.
  2. Transform. i * 2 builds the slot value.
  3. Order. Slots stay in call order: 0 → 2 → 4.
  4. Use. Ideal when each step must compute, not just list numbers.

Example 3 — Default _.identity

Omit the iteratee to get the indexes themselves.

Lodash
_.times(3);
// → [0, 1, 2]

_.times(0);
// → []
Try It Yourself

How It Works

  1. Default. Missing iteratee → _.identity.
  2. Echo. Each index is returned unchanged.
  3. Empty. n = 0 skips the loop and returns [].
  4. Alt. For the same list, _.range(3) is also fine.

Example 4 — Pair with _.constant

Fill slots with the same object reference (official constant pattern).

Lodash
var objects = _.times(2, _.constant({ a: 1 }));

console.log(objects);
// → [{ a: 1 }, { a: 1 }]

console.log(objects[0] === objects[1]);
// → true
Try It Yourself

How It Works

  1. Factory. _.constant({ a: 1 }) closes over one object.
  2. Repeat. _.times(2, fn) calls it twice.
  3. Same ref. Both array slots point to that object.
  4. Caution. Mutating one slot mutates the other — clone if you need isolation.

Example 5 — Build Unique Ids

Generate a list of unique string ids with _.uniqueId.

Lodash
_.times(3, function () {
  return _.uniqueId("row_");
});
// → ["row_1", "row_2", "row_3"]  (numbers grow per process)
Try It Yourself

How It Works

  1. Ignore index. The iteratee does not need i here.
  2. Unique. Each call to _.uniqueId("row_") bumps a counter.
  3. Prefix. Every id starts with row_.
  4. Array. You get three distinct strings ready for list keys or placeholders.

Browser & Runtime Support

_.times is a Lodash Util helper since 0.1.0. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 0.1.0+

_.times()

Available in [email protected] full builds and the lodash/times modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 0.1+
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
_.times Lodash 0.1.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import times from "lodash/times" for smaller bundles. Use it when you need an array of n computed results keyed by index.

Frequently Asked Questions

_.times(n, iteratee) invokes iteratee n times with the index 0, 1, …, n − 1, and returns an array of each return value. Official: _.times(3, String) → ['0','1','2'].
One argument: the zero-based index. There is no collection element — only the count index.
_.identity. So _.times(3) returns [0, 1, 2] — each index echoed back.
Lodash returns []. The iteratee is never called. No manual guard is required for empty counts.
_.range builds a number sequence from start/end/step. _.times runs a function n times and collects results. Use range for numeric arrays; times when each step computes a value.
Use a for loop when you need break/continue, await per iteration, or heavy imperative control. Use _.times for a compact array-of-results expression.

Next: _.toPath()

Convert a value into a Lodash property path array.

_.toPath() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

11 people found this page helpful