Underscore.js _.toPath() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Object

Definition

Ensures that path is an array (official Underscore docs). Strings (and other non-arrays) are wrapped in a one-element array; arrays pass through unmodified. Stable Object helper (since Underscore 1.8.3) — not deprecated.

Pass the required arguments shown below. Underscore object helpers usually return a new value — check whether the source object is mutated.

Syntax

Call _.toPath. Start with this line.

Underscore
_.toPath(path)
CallWhat it does
_.toPath(path)Primary signature.
_.toPath(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.toPath(path)

Parameters

ParameterDescription
pathRequired. Single key or array of keys / indexes

Return Value

Return ValueDescription
*Always an array of path segments

Underscore.js Examples

Example 1 — Wrap a String Key

Official shape: a single key becomes a one-element array.

Underscore
_.toPath('key');
// → ['key']

_.toPath(0);
// → [0]
Try It Yourself

How It Works

  1. Check type. 'key' is not an array.
  2. Wrap once. Underscore returns ['key'].
  3. Numbers too. 0 becomes [0] for index paths.
  4. Why. Deep helpers can always walk an array of segments.

Example 2 — Array Path Unchanged

Official shape: an existing path array is returned unmodified.

Underscore
var path = ['a', 0, 'b'];
var same = _.toPath(path);

// same → ['a', 0, 'b']
// same === path → true
Try It Yourself

How It Works

  1. Pass an array. Nested keys and indexes already listed.
  2. Array check. Underscore sees an array and stops.
  3. Same reference. No copy — same === path.
  4. Deep walks. _.get can follow a → 0 → b as segments.

Example 3 — Dotted Strings Are Not Split

By default, "a.b.c" is one key name, not three segments.

Underscore
_.toPath('a.b.c');
// → ['a.b.c']   // NOT ['a', 'b', 'c']

var data = { 'a.b.c': 1, a: { b: { c: 2 } } };
_.get(data, 'a.b.c');           // → 1  (literal key)
_.get(data, ['a', 'b', 'c']);   // → 2  (deep walk)
Try It Yourself

How It Works

  1. Wrap only. Default toPath never calls split('.').
  2. Literal key. _.get(data, 'a.b.c') looks up the property named "a.b.c".
  3. Deep path. Use an array ['a','b','c'] to walk nested objects.
  4. Rule. Want depth? Pass an array (or override carefully).

Example 4 — Official Override for Dot Shorthands

The docs show a mixin that splits strings on . (Lodash-style) — use with care.

Underscore
var originalToPath = _.toPath;
_.mixin({
  toPath: function (path) {
    return _.isString(path) ? path.split('.') : originalToPath(path);
  }
});

_.toPath('a.0.b');
// → ['a', '0', 'b']

_.get({ a: [{ b: 5 }] }, 'a.0.b');
// → 5
Try It Yourself

How It Works

  1. Save default. Keep originalToPath for non-strings.
  2. Mixin. Strings run through split('.').
  3. Deep get. 'a.0.b' becomes segments for _.get.
  4. Trade-off. A real key named "a.0.b" is harder to reach — prefer ['a.0.b'] then.

Browser & Runtime Support

_.toPath ships with Underscore.js since 1.8.3. It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.8.3+

_.toPath()

Available in [email protected]+ (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.8.3+
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
_.toPath Underscore 1.8.3+

Bottom line: Safe for production once Underscore ≥ 1.8.3 is loaded. Not a native browser API. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.toPath(path) ensures path is an array. A string (or other non-array) becomes a one-element array; an array is returned unmodified. Example: _.toPath('key') → ['key'].
No by default. _.toPath('a.b.c') returns ['a.b.c'], not ['a','b','c']. Deep dotted shorthands require overriding _.toPath (see the official mixin example).
Internally by _.has, _.get, _.invoke, _.property, _.propertyOf, and iteratee-based helpers — so they all normalize deep paths the same way.
Yes. It is an extension point (like _.iteratee). Override via _.mixin to split dotted strings. The docs warn that some keys can become unreachable afterward.
Non-arrays are wrapped, so _.toPath(0) returns [0]. That lets numeric indexes act as single-key paths.
No. If you pass an array, Underscore returns that same array reference unmodified.

Next: _.has()

Check whether an object has an own property — including deep paths normalized by _.toPath.

_.has() tutorial →

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