JavaScript Object.keys() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Object

Definition

Object.keys(obj) returns an array of the object’s own enumerable string property names. Inherited keys and Symbol keys are skipped.

The order matches the normal enumeration order used by for...in for own properties. Use Object.values for values and Object.entries for [key, value] pairs.

Syntax

Call Object.keys() like this. Start with this line.

JavaScript
Object.keys(obj)
CallWhat it does
Object.keys({ a: 1, b: 2 })Returns ["a", "b"].
Object.keys(["x", "y"])Returns ["0", "1"] — array indexes as strings.
Object.keys(Object.create({ a: 1 }))Returns [] if the object has no own enumerable keys.

Pass an object (or a value that can be converted to an object). Arrays give index strings like "0", "1".

Library declarations
JavaScript
Object.keys(obj)
JavaScript
Object.keys(o: object): string[]

Parameters

ParameterDescription
objRequired. The object whose own enumerable string keys you want.

Return Value

Return ValueDescription
string[]A new array of own enumerable string property names.

JavaScript Examples

List Keys on a Plain Object

Build a small object and print its keys.

JavaScript
var user = {
  name: "Ada",
  age: 36,
  city: "London"
};

console.log(Object.keys(user));
console.log(Object.keys(user).length);
Try It Yourself

How It Works

  1. Own string keys. Object.keys returns the three enumerable own names.
  2. Array result. The return value is a normal array, so .length works.
  3. Use for loops. You can map, filter, or loop over the key list like any other array.

Inherited Keys Are Skipped

Keys on the prototype do not appear in Object.keys.

JavaScript
var proto = { role: "guest" };
var user = Object.create(proto);
user.name = "Ada";

console.log(user.role);
console.log(Object.keys(user));
console.log("role" in user);
Try It Yourself

How It Works

  1. Inherited value. user.role comes from the prototype, so reading it works.
  2. Keys stay own. Object.keys(user) only lists "name".
  3. in sees both. The in operator finds inherited keys too, unlike Object.keys.

Keys on an Array

Arrays are objects. Their enumerable keys are the index strings.

JavaScript
var colors = ["red", "green", "blue"];

console.log(Object.keys(colors));
console.log(Object.keys(colors)[0]);
console.log(typeof Object.keys(colors)[0]);
Try It Yourself

How It Works

  1. Index strings. Keys are "0", "1", "2", not numbers.
  2. Still strings. typeof shows each key is a string.
  3. Compare values. Use Object.values(colors) when you want the items themselves.

Browser & Runtime Support

Object.keys() is an ES5 method and is supported in all modern browsers and Node.js.

✓ ES5 · Baseline

Object.keys()

Available everywhere modern JavaScript runs. A daily helper for looping object properties.

100% Modern browser support
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
Object.keys Excellent

Bottom line: Safe for production. Prefer Object.keys over for...in when you only want own enumerable string keys.

Frequently Asked Questions

An array of the object’s own enumerable string property names.
No. Only own keys. Inherited keys are skipped.
No. Object.keys only returns string keys. Use Object.getOwnPropertySymbols for Symbol keys.
for...in also walks enumerable inherited keys. Object.keys stays on own enumerable string keys.
They are skipped. Use Object.getOwnPropertyNames to include non-enumerable own string names.

Next: preventExtensions()

Stop an object from gaining new properties.

preventExtensions() 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.

8 people found this page helpful