es-toolkit/docs/reference/compat/util/constant.md
2024-10-07 09:59:13 +09:00

823 B
Raw Blame History

constant

::: info This function is only available in es-toolkit/compat for compatibility reasons. It either has alternative native JavaScript APIs or isnt fully optimized yet.

When imported from es-toolkit/compat, it behaves exactly like lodash and provides the same functionalities, as detailed here. :::

Creates a new function that always returns value.

Signature

function constant(): () => undefined;
function constant<T>(value: T): () => T;

Parameters

  • value (T): The value to return from the new function.

Returns

(() => T | undefined): The new constant function.

Examples

const object = { a: 1 };
const returnsObject = constant(object);

returnsObject(); // => { a: 1 }
returnsObject() === object; // => true