Lodash _.keys() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Creates an array of the own enumerable property names of object. Part of Lodash Object (stable — not deprecated). Non-object values are coerced to objects. Inherited prototype keys are skipped — use _.keysIn for those. Note: iteration order is not guaranteed by the official docs.

Pass the object and path or keys shown below. Check whether the helper mutates or returns a new object.

Syntax

Call _.keys. Start with this line.

Lodash
_.keys(object)
CallWhat it does
_.keys(object)Primary signature.

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

Library declarations
Lodash
_.keys(object)

Parameters

ParameterDescription
objectRequired. Object to query (non-objects are coerced)

Return Value

Return ValueDescription
*Array of own enumerable property names

Lodash Examples

Example 1 — Official: Own Keys on an Instance

Prototype properties are not listed.

Lodash
function Foo() {
  this.a = 1;
  this.b = 2;
}
Foo.prototype.c = 3;

console.log(_.keys(new Foo()));
// ['a', 'b']
Try It Yourself

How It Works

  1. Own. Instance sets a and b.
  2. Inherited. Prototype adds c.
  3. keys. Returns only own names → ['a', 'b'].
  4. Docs. Matches the official first example.

Example 2 — Official: String Coercion

Non-objects are coerced; character indexes become keys.

Lodash
console.log(_.keys("hi"));
// ['0', '1']
Try It Yourself

How It Works

  1. Coerce. 'hi' is treated like an object.
  2. Indexes. Characters live at 0 and 1.
  3. Result. String keys ['0', '1'].
  4. Docs. Matches the official second example.

Example 3 — Plain Object Keys

The everyday case — list every own field name.

Lodash
var user = { id: 1, name: "Ada", active: true };

console.log(_.keys(user));
// ['id', 'name', 'active']
Try It Yourself

How It Works

  1. Own fields. All three properties are enumerable.
  2. keys. Returns their names as an array.
  3. Use. Forms, serializers, and dynamic loops.
  4. Pair. Combine with _.values when you need the data too.

Example 4 — _.keys vs _.keysIn

Choose own-only vs own-or-inherited.

Lodash
function Foo() {
  this.a = 1;
  this.b = 2;
}
Foo.prototype.c = 3;

var obj = new Foo();

console.log(_.keys(obj));   // ['a', 'b']
console.log(_.keysIn(obj)); // ['a', 'b', 'c']
Try It Yourself

How It Works

  1. keys. Own only → skips c.
  2. keysIn. Walks the prototype → includes c.
  3. When. Prefer keys for “what did this object define?”
  4. When. Prefer keysIn for “what can I enumerate?”

Example 5 — Nullish Values → Empty Array

Lodash stays friendly where native Object.keys throws.

Lodash
console.log(_.keys(null));      // []
console.log(_.keys(undefined)); // []
console.log(_.keys(42));        // []
Try It Yourself

How It Works

  1. null / undefined. No properties → [].
  2. Number. Coerced object has no enumerable own keys → [].
  3. Contrast. Object.keys(null) throws a TypeError.
  4. Use. Safer default for uncertain API payloads.

Browser & Runtime Support

_.keys is a Lodash Object helper available in 4.x (and earlier builds). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native Object.keys is similar for plain objects; Lodash adds non-object coercion and null-safe empty arrays.

✓ Stable · Lodash 4.x

_.keys()

Available in [email protected] full builds and the lodash/keys 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
_.keys Lodash 4.x

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import keys from "lodash/keys" for smaller builds. Use keysIn when inherited keys should be included.

Frequently Asked Questions

_.keys(object) creates an array of the own enumerable property names of object. Official: _.keys(new Foo) → ['a', 'b'] (prototype key c is skipped).
_.keys returns only own enumerable string keys. _.keysIn also includes inherited enumerable string keys from the prototype chain.
For ordinary objects they are very similar. Lodash coerces non-objects and is friendlier with null/undefined (empty array instead of a throw).
Strings are coerced to objects. Character indexes become own enumerable keys — official docs show exactly that.
No. It returns string-keyed property names only.
Use import keys from "lodash/keys" or require("lodash/keys") so bundlers can tree-shake. Full build: import _ from "lodash" then _.keys.

Next: _.keysIn()

List own and inherited enumerable property names.

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