es-toolkit/docs/reference/compat/util/toNumber.md
2024-09-28 22:22:34 +09:00

847 B
Raw Blame History

toNumber

::: 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. :::

Converts value to a number.

Unlike Number(), this function returns NaN for symbols.

Signature

function toNumber(value?: unknown): number;

Parameters

  • value (unknown): The value to convert.

Returns

(number): The converted number.

Examples

toNumber(3.2); // => 3.2
toNumber(Number.MIN_VALUE); // => 5e-324
toNumber(Infinity); // => Infinity
toNumber('3.2'); // => 3.2
toNumber(Symbol.iterator); // => NaN
toNumber(NaN); // => NaN