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])
Call
What 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
Parameter
Description
n
Required. How many times to call the iteratee
iteratee
Required. Function (index) → value
context
Optional. Sets this inside the iteratee
Return Value
Return Value
Description
*
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]
Context. Underscore calls the iteratee with genie as this.
Prefix.this.prefix is "wish " on every call.
Index. Appended as 0, 1, 2.
Arrow warning. Arrow functions ignore this binding — use a function expression.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.timesUnderscore 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.