Lodash _.constant() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Util

Definition

Creates a function that returns value. Part of Lodash Util since 2.4.0 (stable — not deprecated).

Note: The returned function ignores all arguments. Objects and arrays are returned by the same reference — not cloned.

Syntax

Call _.constant. Start with this line.

Lodash
_.constant(value)
CallWhat it does
_.constant(value)Primary signature.
_.constant(...)Call the util helper.

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

Library declarations
Lodash
_.constant(value)

Parameters

ParameterDescription
valueRequired. Value returned by the new function

Return Value

Return ValueDescription
*New function that always returns value

Lodash Examples

Example 1 — Official: _.times + Shared Object

Build an array of identical object references with _.times.

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 that function twice.
  3. Same ref. Both slots point to the identical object.
  4. Check. === is true — not two clones.

Example 2 — Fixed Number

Create a function that always returns 42.

Lodash
var always42 = _.constant(42);

console.log(always42());
// → 42

console.log(always42());
// → 42
Try It Yourself

How It Works

  1. Capture. 42 is stored when you call _.constant.
  2. Call. always42() returns that number.
  3. Again. Every later call returns the same 42.
  4. Use. Handy wherever a callback must return a fixed primitive.

Example 3 — Arguments Are Ignored

Extra arguments never change the return value.

Lodash
var alwaysOk = _.constant("OK");

console.log(alwaysOk());
// → "OK"

console.log(alwaysOk("nope", 99));
// → "OK"
Try It Yourself

How It Works

  1. Close over. "OK" is fixed at creation time.
  2. No-arg call. Returns "OK".
  3. With args. Still returns "OK" — callers cannot override it.
  4. Contrast. Unlike _.identity, inputs do not pass through.

Example 4 — Default Branch in _.cond

Use fixed handlers (and _.stubTrue) for a catch-all result.

Lodash
var labelSize = _.cond([
  [function (n) { return n < 10; }, _.constant("Small")],
  [function (n) { return n < 20; }, _.constant("Medium")],
  [_.stubTrue, _.constant("Huge")]
]);

console.log(labelSize(5));
// → "Small"

console.log(labelSize(35));
// → "Huge"
Try It Yourself

How It Works

  1. Pairs. Each row is [predicate, handler].
  2. Match. 5 is < 10 → handler returns "Small".
  3. Fallback. _.stubTrue always matches; handler returns "Huge".
  4. Why constant. Handlers must be functions; fixed strings need a wrapper.

Example 5 — constant(true) vs stubTrue

Stub helpers behave like specialized constants for common values.

Lodash
var alwaysTrue = _.constant(true);

console.log(alwaysTrue());
// → true

console.log(_.stubTrue());
// → true

console.log(_.stubFalse());
// → false
Try It Yourself

How It Works

  1. General. _.constant(true) builds any fixed return.
  2. Named stubs. _.stubTrue / _.stubFalse read clearly for booleans.
  3. Same idea. Both ignore arguments and return a fixed value.
  4. Prefer. Use stubs for true/false; use constant for custom payloads.

Browser & Runtime Support

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

✓ Stable · Lodash 2.4.0+

_.constant()

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

100% With Lodash 2.4+
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 Lodash 2.4.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import constant from "lodash/constant" for smaller bundles. Use it when an API expects a function but the result never changes.

Frequently Asked Questions

_.constant(value) creates a function that returns value every time it is called. Official: _.times(2, _.constant({a:1})) builds two slots that share the same object.
No. The returned function ignores every argument. always42(100) still returns 42.
No. Objects and arrays are returned by reference. That is why objects[0] === objects[1] is true in the official example.
_.identity(x) returns x (the argument). _.constant(v) ignores args and always returns the captured v.
_.stubTrue() is like _.constant(true)(). Stub helpers are specialized constant-style functions for common fixed returns.
Use import constant from "lodash/constant" or require("lodash/constant"). Full build: import _ from "lodash" then _.constant.

Next: _.defaultTo()

Return a default when a value is null, undefined, or NaN.

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