Underscore.js _.result Property

Beginner
5 min read
Updated: Nov 2026
5 examples
Utility

Definition

If the named property is a function, invoke it with the object as context; otherwise return it (official Underscore docs). Built for APIs that allow either a raw value or a function that produces one.

Pass the required arguments shown below. Underscore utility helpers usually return a new value or function — check edge cases before you ship.

Syntax

Read _.result. Start with this line.

Underscore
_.result(object, property, [defaultValue])
CallWhat it does
_.result(object, property, [defaultValue])Primary signature.
_.result(...)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
_.result(object, property, [defaultValue])

Parameters

ParameterDescription
objectRequired. Source to resolve from
propertyRequired. Key string, or array of keys for nesting
defaultValueOptional. Used when the resolved value is undefined

Return Value

Return ValueDescription
*Property value, function return, or default

Underscore.js Examples

Example 1 — Official Docs Example

A string property, a function property, and a missing key with a default.

Underscore
var object = {
  cheese: "crumpets",
  stuff: function () { return "nonsense"; }
};

_.result(object, "cheese");       // → "crumpets"
_.result(object, "stuff");        // → "nonsense"
_.result(object, "meat", "ham");  // → "ham"
Try It Yourself

How It Works

  1. Read a value. cheese is a string → returned as-is.
  2. Call a function. stuff is invoked → "nonsense".
  3. Missing key. meat is undefined → third argument "ham" wins.
  4. Why it exists. One helper for “value or method” APIs.

Example 2 — Invoke with the Object as this

The method can read other keys on the same object.

Underscore
var object = {
  cheese: "crumpets",
  lunch: function () { return this.cheese; }
};

_.result(object, "lunch");
// → "crumpets"
Try It Yourself

How It Works

  1. Find the method. lunch is a function on object.
  2. Call with context. Underscore does lunch.call(object).
  3. Read siblings. Inside the method, this.cheese is "crumpets".
  4. Arrow warning. Arrow functions ignore .call — use a function expression.

Example 3 — A Function as defaultValue

If the property is missing and the default is a function, Underscore calls it.

Underscore
var object = { cheese: "crumpets" };

_.result(object, "meat", function () {
  return this.cheese + " instead";
});
// → "crumpets instead"

_.result(object, "cheese", "ham");
// → "crumpets"  (default ignored)
Try It Yourself

How It Works

  1. Missing key. meat is undefined → fallback runs.
  2. Function default. Called with the object as this (since 1.8.0).
  3. Present key. cheese exists → "ham" is never used.
  4. Lazy work. Expensive fallbacks only run when needed.

Example 4 — Deep Path vs Dotted String

Pass an array of keys to walk nested objects. A string with a dot is still one key.

Underscore
var object = { kitchen: { cheese: "crumpets" } };

_.result(object, ["kitchen", "cheese"]);
// → "crumpets"

_.result(object, "kitchen.cheese");
// → undefined  (looks up key "kitchen.cheese")
Try It Yourself

How It Works

  1. Array path. Walk kitchen, then cheese.
  2. String path. _.toPath("kitchen.cheese") is ["kitchen.cheese"].
  3. No match. That dotted key does not exist → undefined.
  4. Rule. Use arrays for nesting unless you override _.toPath.

Example 5 — _.result vs _.get

Along a path, result calls functions. get does not.

Underscore
var object = {
  kitchen: function () {
    return { cheese: "crumpets" };
  }
};

_.result(object, ["kitchen", "cheese"]);
// → "crumpets"

_.get(object, ["kitchen", "cheese"]);
// → undefined  (kitchen is a function, not an object)
Try It Yourself

How It Works

  1. result. Invokes kitchen, then reads cheese → "crumpets".
  2. get. Treats kitchen as a function value — no child keys.
  3. When to use result. Path may include methods you want called.
  4. When to use get. You only want to read, never invoke.

Browser & Runtime Support

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

✓ Stable · Underscore 1.x

_.result()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded. Added in 1.3.3; defaultValue in 1.8.0.

100% With Underscore 1.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
_.result Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.result(object, property, [defaultValue]) reads a property. If the value is a function, Underscore invokes it with the object as this and returns that result. If the property is missing or undefined, the optional default is used. If the default is a function, it is invoked too.
var object = {cheese: 'crumpets', stuff: function(){ return 'nonsense'; }}; then _.result(object, 'cheese') is 'crumpets', _.result(object, 'stuff') is 'nonsense', and _.result(object, 'meat', 'ham') is 'ham'.
When the resolved value is undefined — the key is missing, or it is explicitly undefined. null, false, 0, and '' are kept. If defaultValue is a function, Underscore calls it with the parent object as this.
_.get returns whatever sits at the path, including a function you did not call. _.result invokes functions it finds, including along a deep array path. That is why the docs mention APIs that allow either functions or raw properties.
Yes, pass an array of keys such as ['kitchen', 'cheese']. A string is wrapped as a single key, so 'kitchen.cheese' looks up a key with a dot in the name, not a nested path, unless you override _.toPath.
No. It is a stable Underscore utility added in version 1.3.3 (April 2012). Version 1.8.0 added the optional fallback value or function.

Next: _.now()

Get a high-resolution integer timestamp for timing and animation.

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

11 people found this page helpful