Named stubs._.stubTrue / _.stubFalse read clearly for booleans.
Same idea. Both ignore arguments and return a fixed value.
Prefer. Use stubs for true/false; use constant for custom payloads.
Compatibility
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 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
_.constantLodash 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.