Lodash Constant Function Module

3.0.0 · maintenance · verified Tue Apr 21

lodash.constant is a standalone npm package providing the `_.constant` utility function, designed for the Lodash v3 ecosystem. It delivers a function that, when called, consistently returns the same, unchanging value that was initially passed to it. This module, currently at version 3.0.0, was last published in January 2015, effectively freezing its functionality to align with the Lodash 3.x release series. While stable for older CommonJS environments, it is not actively maintained and does not receive updates or security patches from the ongoing development of the main Lodash library (which is currently at v4.x and has a healthy, active release cadence). For modern applications targeting Lodash v4+, direct imports like `import constant from 'lodash/constant';` or using `lodash-es` for ES module compatibility and tree-shaking are the recommended approaches, making this standalone module largely a legacy solution.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `constant` function, showing its consistent return value and immutability.

import constant from 'lodash.constant';

const getAnswer = constant(42);
const getGreeting = constant('Hello, World!');
const getObject = constant({ id: 1, name: 'Test' });

console.log('The answer is:', getAnswer()); // Expected: The answer is: 42
console.log('The greeting is:', getGreeting()); // Expected: The greeting is: Hello, World!
console.log('The object is:', getObject()); // Expected: The object is: { id: 1, name: 'Test' }

// Demonstrating immutability for constants, modifying the returned object does not affect future calls
const myObject = getObject();
myObject.name = 'Modified';
console.log('Original object after modification attempt:', getObject()); // Still { id: 1, name: 'Test' }
console.log('Modified object:', myObject); // Expected: { id: 1, name: 'Modified' }

view raw JSON →