Underscore.js _.keys() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Object

Definition

Retrieve all the names of the object’s own enumerable properties (official Underscore docs). Returns a new array of string keys. Stable helper — 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 _.keys. Start with this line.

Underscore
_.keys(object)
CallWhat it does
_.keys(object)Primary signature.
_.keys(...)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
_.keys(object)

Parameters

ParameterDescription
objectRequired. Source to read keys from

Return Value

Return ValueDescription
*New array of string key names

Underscore.js Examples

Example 1 — Official: Three Keys

The docs list the own property names of a small object.

Underscore
_.keys({one: 1, two: 2, three: 3});
// → ["one", "two", "three"]
Try It Yourself

How It Works

  1. Source object. Three own properties: one, two, three.
  2. Collect names. Underscore pushes each enumerable own key into a new array.
  3. Result. ["one", "two", "three"] — the official docs example.
  4. Safe. The original object is not modified.

Example 2 — Own Keys vs Inherited

_.keys skips prototype properties; _.allKeys includes them.

Underscore
function Stooge(name) {
  this.name = name;
}
Stooge.prototype.silly = true;

var moe = new Stooge("Moe");

_.keys(moe);     // → ["name"]
_.allKeys(moe);  // → ["name", "silly"]
Try It Yourself

How It Works

  1. Own field. name is set on the instance.
  2. Prototype field. silly lives on Stooge.prototype.
  3. keys. Only own enumerable names → ["name"].
  4. allKeys. Own plus inherited enumerable names — see _.allKeys.

Example 3 — Pair Keys with Values

Keys and values line up by index for the same object.

Underscore
var obj = {one: 1, two: 2, three: 3};

_.keys(obj);    // → ["one", "two", "three"]
_.values(obj);  // → [1, 2, 3]
_.pairs(obj);   // → [["one", 1], ["two", 2], ["three", 3]]
Try It Yourself

How It Works

  1. Names. _.keys lists the property names.
  2. Values. _.values lists the matching values in the same property order.
  3. Pairs. _.pairs combines each name with its value.
  4. Pattern. Use keys when you need names; pairs when you need both.

Example 4 — Empty Object & Non-Object

No own keys means an empty array; non-objects are handled safely.

Underscore
_.keys({});          // → []
_.keys(null);        // → []
_.keys(42);          // → []
_.keys({a: 1});      // → ["a"]
Try It Yourself

How It Works

  1. Empty object. Nothing to list → [].
  2. null / number. Underscore treats non-objects safely → [].
  3. One key. {a: 1} returns ["a"].
  4. Guard tip. Still prefer _.isObject when you want an explicit type check first.

Browser & Runtime Support

_.keys 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. On modern engines it aligns closely with native Object.keys.

✓ Stable · Underscore 1.x

_.keys()

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

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
_.keys Underscore 1.x+

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

Frequently Asked Questions

_.keys(object) returns an array of the object's own enumerable property names. Example: _.keys({one: 1, two: 2, three: 3}) returns ["one", "two", "three"].
No. It lists only own enumerable properties. Use _.allKeys when you also need enumerable names from the prototype chain.
_.keys returns property names; _.values returns the matching values. Indexes line up: keys[i] pairs with values[i].
For plain objects they match closely. Underscore adds safer handling for non-objects (often returning []) and older-engine consistency. On modern JavaScript, Object.keys is a fine native alternative.
_.keys (like Object.keys) returns string keys only. Symbol keys are omitted — use Object.getOwnPropertySymbols for those.
No. It is a stable Underscore object helper. Load Underscore 1.x (CDN or npm) and call _.keys(object).

Next: _.allKeys()

List own and inherited enumerable property names — useful with prototypes.

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