Lodash _.defaultTo() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Util

Definition

Checks value and returns defaultValue when value is NaN, null, or undefined. Part of Lodash Util since 4.14.0 (stable — not deprecated).

Note: Unlike ||, it keeps 0, '', and false. Unlike ??, it also replaces NaN.

Syntax

Call _.defaultTo. Start with this line.

Lodash
_.defaultTo(value, defaultValue)
CallWhat it does
_.defaultTo(value, defaultValue)Primary signature.
_.defaultTo(...)Call the util helper.

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

Library declarations
Lodash
_.defaultTo(value, defaultValue)

Parameters

ParameterDescription
valueRequired. Value to check
defaultValueRequired. Returned when value is null, undefined, or NaN

Return Value

Return ValueDescription
*Resolved value (original or default)

Lodash Examples

Example 1 — Official: Keep Value or Use Default

Pass a present value, then an absent one.

Lodash
_.defaultTo(1, 10);
// → 1

_.defaultTo(undefined, 10);
// → 10
Try It Yourself

How It Works

  1. Present. 1 is fine → return 1.
  2. Missing. undefined triggers the default.
  3. Result. Second call yields 10.
  4. Pattern. Same idea for API fields that may be absent.

Example 2 — null Fallback

null is treated like a missing value.

Lodash
_.defaultTo(null, "Guest");
// → "Guest"

_.defaultTo("Ada", "Guest");
// → "Ada"
Try It Yourself

How It Works

  1. null check. null == null → use default.
  2. Fallback. Returns "Guest".
  3. Real name. "Ada" is kept.
  4. Use. Typical display-name or label defaulting.

Example 3 — Keep 0 and ''

Falsy-but-valid values are not replaced.

Lodash
_.defaultTo(0, 10);
// → 0

_.defaultTo("", "n/a");
// → ""
Try It Yourself

How It Works

  1. Zero. 0 is neither nullish nor NaN → kept.
  2. Empty string. Same rule → kept.
  3. Why it matters. Counts and blank text stay meaningful.
  4. vs ||. 0 || 10 would wrongly become 10.

Example 4 — NaN Gets the Default

Lodash also treats NaN as a trigger (unlike ??).

Lodash
_.defaultTo(NaN, 10);
// → 10

_.defaultTo(Number("oops"), 0);
// → 0
Try It Yourself

How It Works

  1. NaN test. Internally uses value !== value.
  2. Fallback. NaN → 10.
  3. Parse fail. Number("oops") is NaN → 0.
  4. vs ??. NaN ?? 10 stays NaN; Lodash replaces it.

Example 5 — defaultTo vs ||

See how || over-replaces valid zeros.

Lodash
_.defaultTo(0, 10);
// → 0

0 || 10;
// → 10

_.defaultTo(null, 10);
// → 10

null || 10;
// → 10
Try It Yourself

How It Works

  1. defaultTo + 0. Keeps a real zero.
  2. || + 0. Treats zero as falsy → 10.
  3. null. Both helpers fall back to 10.
  4. Choose. Prefer defaultTo (or ??) when 0 is valid.

Browser & Runtime Support

_.defaultTo is a Lodash Util helper since 4.14.0. It runs anywhere Lodash 4.14+ runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.14.0+

_.defaultTo()

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

100% With Lodash 4.14+
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
_.defaultTo Lodash 4.14.0+

Bottom line: Safe for production once Lodash 4.14+ is loaded. Prefer import defaultTo from "lodash/defaultTo" for smaller bundles. Use it when 0 and empty strings must survive, but NaN should still fall back.

Frequently Asked Questions

_.defaultTo(value, defaultValue) returns value unless value is null, undefined, or NaN — then it returns defaultValue. Official: _.defaultTo(1, 10) → 1; _.defaultTo(undefined, 10) → 10.
No. 0, '', and false are kept. Only null, undefined, and NaN trigger the fallback.
|| replaces every falsy value (0, '', false, null, undefined, NaN). _.defaultTo keeps 0 and '' — safer for counts and text fields.
?? falls back only for null and undefined. _.defaultTo also falls back for NaN. That NaN check is the main Lodash difference.
_.defaultTo works on one value. _.defaults fills missing undefined keys on an object from source objects.
Use import defaultTo from "lodash/defaultTo" or require("lodash/defaultTo"). Full build: import _ from "lodash" then _.defaultTo.

Next: _.flow()

Compose functions left-to-right into one pipeline.

_.flow() 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