JS: Object.keys

JS: Object.keys

Object.keys ( obj )

Return a array of string property keys that are own and enumerable. (no symbol keys, and enumerable only)

[see JS: Property Attributes, writable, enumerable, configurable]

[see JS: Symbol Tutorial]

[see JS: List Properties]

// get an array of enumerable own string properties of a object

const obj = {"a":3, "b":4};

console.log ( Object.keys(obj) ); // [ 'a', 'b' ]

The following example shows that symbol key and non-enumerable properties are ignored.

const s = Symbol("x");

const u =
Object.create ( Object.prototype, {
    "p": { value : 3, writable: true, enumerable: false, configurable: true },
    "q": { value : 3, writable: true, enumerable: true, configurable: true },
    [Symbol()]: { value : 3, writable: true, enumerable: true, configurable: true },
});

// get all own string property keys that are enumerable
console.log ( Object.keys ( u ) ); // ['q']

// symbol key and non-enumerable properties are ignored
console.log ( Reflect.ownKeys ( u ) ); // [ 'p', 'q', Symbol() ]

Reference

ECMAScript 2015 §Fundamental Objects#sec-object.keys

Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc