Underscore.js _.constant() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Utility

Definition

Creates a function that returns the same value used as the argument of _.constant (official Underscore docs). A small predicate-generating helper — often useful outside Underscore too.

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

Syntax

Call _.constant. Start with this line.

Underscore
_.constant(value)
CallWhat it does
_.constant(value)Primary signature.
_.constant(...)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
_.constant(value)

Parameters

ParameterDescription
valueRequired. What every later call should return

Return Value

Return ValueDescription
*A new function (not value itself)

Underscore.js Examples

Example 1 — Official Stooge Check

The returned function yields the same object reference.

Underscore
var stooge = { name: 'moe' };

stooge === _.constant(stooge)();
// → true
Try It Yourself

How It Works

  1. Build an object. stooge holds { name: 'moe' }.
  2. Wrap it. _.constant(stooge) returns a function.
  3. Call it. The trailing () returns that same object.
  4. Same reference. === is true — not a clone.

Example 2 — Fill a List with One Value

Pass a constant function to _.map when every slot should be the same.

Underscore
var fillZero = _.constant(0);

_.map([1, 2, 3, 4], fillZero);
// → [0, 0, 0, 0]

_.times(3, _.constant('hi'));
// → ['hi', 'hi', 'hi']
Try It Yourself

How It Works

  1. Make a filler. fillZero always returns 0.
  2. Map over items. Each element is replaced by that fixed value.
  3. Times too. _.times calls the function three times → three 'hi's.
  4. Why constant. APIs want a function; you want a fixed answer.

Example 3 — Later Arguments Are Ignored

Extra arguments on the returned function do not change the result.

Underscore
var alwaysYes = _.constant(true);

alwaysYes();
// → true

alwaysYes(false, 'nope', 99);
// → true
Try It Yourself

How It Works

  1. Lock the value. true is captured when constant runs.
  2. Call with nothing. Still returns true.
  3. Call with junk. Arguments are ignored — still true.
  4. Use case. Safe default predicate that never flips.

Example 4 — _.constant vs _.identity

Identity returns now; constant returns a function for later.

Underscore
_.identity(10);
// → 10

var ten = _.constant(10);
ten();
// → 10

typeof _.identity(10);   // "number"
typeof _.constant(10);   // "function"
Try It Yourself

How It Works

  1. Identity. Hands the argument straight back.
  2. Constant. Hands back a function that will return 10.
  3. Types differ. One is the value; the other is a wrapper function.
  4. Rule. Need a value now → identity. Need a fixed callback → constant.

Browser & Runtime Support

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

_.constant()

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

_.constant(value) returns a new function. Every later call — with zero or many arguments — returns that same value. Arguments on those later calls are ignored.
var stooge = {name: 'moe'}; stooge === _.constant(stooge)() is true. The extra () matters: constant returns a function; you call that function to get stooge.
_.identity(value) returns value immediately. _.constant(value) returns a function that will return value later. Underscore describes constant as a higher-order identity.
No. If you pass an object, every call returns the same reference. Mutating that object changes what future calls see.
When an API expects a function but you only need a fixed return — default callbacks, _.times / _.map fillers, or a named always-this-value helper.
No. It is a stable Underscore utility (predicate-generating / higher-order identity), available in Underscore 1.x.

Next: _.noop()

Learn the empty function Underscore uses when you need a callback that does nothing.

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