Underscore.js _.times() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Utility

Definition

Invokes the given iteratee function n times. Each call receives an index argument. Produces an array of the returned values (official Underscore docs).

Pass the required arguments shown below. Underscore utility helpers usually return a new value or function — check edge cases before you ship.

Syntax

Call _.times. Start with this line.

Underscore
_.times(n, iteratee, [context])
CallWhat it does
_.times(n, iteratee, [context])Primary signature.
_.times(...)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
_.times(n, iteratee, [context])

Parameters

ParameterDescription
nRequired. How many times to call the iteratee
iterateeRequired. Function (index) → value
contextOptional. Sets this inside the iteratee

Return Value

Return ValueDescription
*Array of iteratee results (length n)

Underscore.js Examples

Example 1 — Official Genie Loop

Call an iteratee three times with indexes 0, 1, and 2.

Underscore
var genie = {
  granted: [],
  grantWishNumber: function (n) {
    this.granted.push(n);
  }
};

_.times(3, function (n) {
  genie.grantWishNumber(n);
});
// genie.granted → [0, 1, 2]
Try It Yourself

How It Works

  1. Count. n = 3 means three calls.
  2. Indexes. The iteratee receives 0, then 1, then 2.
  3. Side effect. Each call pushes that index onto genie.granted.
  4. Return array. Still built — here each slot is undefined because nothing was returned.

Example 2 — Collect Results

Return a value from the iteratee. _.times builds an array of those values.

Underscore
_.times(3, function (n) {
  return n * n;
});
// → [0, 1, 4]
Try It Yourself

How It Works

  1. Index 0. 0 * 0 = 0.
  2. Index 1. 1 * 1 = 1.
  3. Index 2. 2 * 2 = 4.
  4. Array. Results land in order: [0, 1, 4].

Example 3 — _.times vs _.range

Same numbers, different jobs — times runs a function; range only builds numbers.

Underscore
_.times(3, _.identity);  // → [0, 1, 2]
_.range(3);               // → [0, 1, 2]
Try It Yourself

How It Works

  1. identity. _.identity returns its argument — the index.
  2. Same shape. Both calls produce [0, 1, 2].
  3. Choose range when you only need numbers.
  4. Choose times when each index must compute something else.

Example 4 — Fill with a Constant

Pair times with _.constant to fill n slots with one value.

Underscore
_.times(3, _.constant("ok"));
// → ["ok", "ok", "ok"]
Try It Yourself

How It Works

  1. constant. Returns a function that always yields "ok".
  2. Three calls. Index is ignored; each return is "ok".
  3. Fill pattern. Handy for default rows or placeholder lists.
  4. Length. Result length always matches n.

Example 5 — Optional Context

Pass a third argument so this inside the iteratee is that object.

Underscore
var genie = { prefix: "wish " };

_.times(3, function (n) {
  return this.prefix + n;
}, genie);
// → ["wish 0", "wish 1", "wish 2"]
Try It Yourself

How It Works

  1. Context. Underscore calls the iteratee with genie as this.
  2. Prefix. this.prefix is "wish " on every call.
  3. Index. Appended as 0, 1, 2.
  4. Arrow warning. Arrow functions ignore this binding — use a function expression.

Browser & Runtime Support

_.times 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

_.times()

Available in [email protected] (UMD / ESM). Added in 0.6.0; returns a results array since 1.4.3. 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
_.times 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]).

Frequently Asked Questions

_.times(n, iteratee, [context]) calls iteratee n times. Each call receives the current index (0, 1, …, n − 1). It returns an array of whatever the iteratee returns.
_.times(3, function(n){ genie.grantWishNumber(n); }) runs the iteratee three times with 0, then 1, then 2. The docs use it as a counted loop; the return array is still produced.
One argument: the zero-based index of this call. There is no collection item — only the count. Pass an optional context as the third argument to set this.
range builds a list of numbers. times runs a function n times and collects return values. _.times(3, _.identity) and _.range(3) both yield [0, 1, 2], but times can compute any value per index.
Underscore returns an empty array and does not call the iteratee. Pass a non-negative integer for n.
No. It is a stable Underscore utility added in version 0.6.0 (February 2010). Since 1.4.3 it returns the mapped array of iteratee results.

Next: _.random()

Generate a random integer — useful with times for mock data.

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