Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Object.keys()

BaselineWidely available

TheObject.keys() static method returns an array of a given object's own enumerable string-keyed property names.

Try it

const object1 = {  a: "some string",  b: 42,  c: false,};console.log(Object.keys(object1));// Expected output: Array ["a", "b", "c"]

Syntax

js
Object.keys(obj)

Parameters

obj

An object.

Return value

An array of strings representing the given object's own enumerable string-keyed property keys.

Description

Object.keys() returns an array whose elements are strings corresponding to the enumerable string-keyed property names found directly uponobject. This is the same as iterating with afor...in loop, except that afor...in loop enumerates properties in the prototype chain as well. The order of the array returned byObject.keys() is the same as that provided by afor...in loop.

If you need the property values, useObject.values() instead. If you need both the property keys and values, useObject.entries() instead.

Examples

Using Object.keys()

js
// Basic arrayconst arr = ["a", "b", "c"];console.log(Object.keys(arr)); // ['0', '1', '2']// Array-like objectconst obj = { 0: "a", 1: "b", 2: "c" };console.log(Object.keys(obj)); // ['0', '1', '2']// Array-like object with random key orderingconst anObj = { 100: "a", 2: "b", 7: "c" };console.log(Object.keys(anObj)); // ['2', '7', '100']// getFoo is a non-enumerable propertyconst myObj = Object.create(  {},  {    getFoo: {      value() {        return this.foo;      },    },  },);myObj.foo = 1;console.log(Object.keys(myObj)); // ['foo']

If you wantall string-keyed own properties, including non-enumerable ones, seeObject.getOwnPropertyNames().

Using Object.keys() on primitives

Non-object arguments arecoerced to objects.undefined andnull cannot be coerced to objects and throw aTypeError upfront. Only strings may have own enumerable properties, while all other primitives return an empty array.

js
// Strings have indices as enumerable own propertiesconsole.log(Object.keys("foo")); // ['0', '1', '2']// Other primitives except undefined and null have no own propertiesconsole.log(Object.keys(100)); // []

Note:In ES5, passing a non-object toObject.keys() threw aTypeError.

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-object.keys

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp