Lodash _.toNumber() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Lang

Definition

Converts value to a number — the raw coercion under _.toFinite, _.toInteger, and friends. Can return NaN or ±Infinity. Part of Lodash 4.x Lang (since 4.0.0).

Pass the value you want to inspect or convert. Read the return type carefully before you use the result.

Syntax

Call _.toNumber. Start with this line.

Lodash
_.toNumber(value)
CallWhat it does
_.toNumber(value)Primary signature.

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

Library declarations
Lodash
_.toNumber(value)

Parameters

ParameterDescription
valueRequired. Any value to convert

Return Value

Return ValueDescription
*number (may be NaN / ±Infinity)

Lodash Examples

Example 1 — Docs: Number and Numeric String

Decimals stay decimals; numeric strings parse cleanly.

Lodash
_.toNumber(3.2);
// → 3.2

_.toNumber("3.2");
// → 3.2
Try It Yourself

How It Works

  1. Number. Already a number → returned as-is.
  2. String. Trimmed and parsed to 3.2.
  3. No truncate. Unlike toInteger, the fraction stays.
  4. Docs. Matches the common Lodash examples.

Example 2 — Docs: MIN_VALUE and Infinity

Tiny and infinite values are preserved — no sanitizing.

Lodash
_.toNumber(Number.MIN_VALUE);
// → 5e-324

_.toNumber(Infinity);
// → Infinity
Try It Yourself

How It Works

  1. MIN_VALUE. Already a finite number → kept.
  2. Infinity. Already a number → kept (not clamped).
  3. Contrast. toFinite(Infinity) would be MAX_VALUE.
  4. Role. This is the primitive; sanitizers wrap it.

Example 3 — null, undefined, Booleans, Symbol

Common myth: “nullish always becomes 0.” Only null does.

Lodash
_.toNumber(null);
// → 0

_.toNumber(undefined);
// → NaN

_.toNumber("");
// → 0

_.toNumber(true);
// → 1

_.toNumber(Symbol());
// → NaN
Try It Yourself

How It Works

  1. null. Same as +null → 0.
  2. undefined. Same as +undefined → NaN.
  3. Empty string. Parses as 0.
  4. Symbol. Short-circuits to NaN (no throw).

Example 4 — Prefixes, Trim, and valueOf

Literal prefixes and object hooks are the extras students ask about.

Lodash
_.toNumber("  7  ");
// → 7

_.toNumber("0x1f");
// → 31

_.toNumber("0b11");
// → 3

_.toNumber("0o17");
// → 15

_.toNumber("-0x10");
// → NaN

_.toNumber({ valueOf: function () { return 7; } });
// → 7
Try It Yourself

How It Works

  1. Trim. Leading/trailing spaces are removed first.
  2. Prefixes. Hex, binary, and octal literals parse like JS.
  3. Signed hex. "-0x10" is rejected → NaN.
  4. valueOf. Objects with a numeric valueOf coerce cleanly.

Example 5 — toNumber vs toFinite

Pick the sanitizer when you need a guaranteed finite number.

Lodash
_.toNumber(Infinity);
// → Infinity

_.toFinite(Infinity);
// → 1.7976931348623157e+308

_.toNumber(NaN);
// → NaN

_.toFinite(NaN);
// → 0

_.toNumber("abc");
// → NaN

_.toFinite("abc");
// → 0
Try It Yourself

How It Works

  1. Raw. toNumber reports what coercion produced.
  2. Sanitize. toFinite never returns NaN or Infinity.
  3. Bad strings. Both parse to NaN first; only toFinite maps to 0.
  4. Choose. Diagnostics → toNumber; math defaults → toFinite.

Browser & Runtime Support

_.toNumber is a Lodash 4.x Lang helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Closest natives are Number(value) / unary +; Lodash adds consistent string trimming, literal-prefix parsing, and the valueOf object path.

✓ Stable · Lodash 4.x

_.toNumber()

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

100% With Lodash 4.x
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
_.toNumber Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import toNumber from "lodash/toNumber" for smaller builds. Reach for toFinite when you need a soft, always-finite result.

Frequently Asked Questions

_.toNumber(value) converts value to a JavaScript number. Unlike toFinite, it can return NaN or ±Infinity.
No. undefined → NaN. null → 0. If you want a soft 0 fallback, use _.toFinite.
Very similar for most inputs. Lodash trims strings and carefully parses 0x / 0b / 0o prefixes; objects go through valueOf first.
toNumber keeps NaN and Infinity. toFinite maps those to 0 and ±Number.MAX_VALUE.
Symbol becomes NaN. BigInt throws — convert explicitly first if you must.
Use import toNumber from "lodash/toNumber" or require("lodash/toNumber") for tree-shaking. Full build: import _ from "lodash" then _.toNumber.

Next: _.toPlainObject()

Flatten inherited enumerable properties onto a plain object.

_.toPlainObject() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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