Get Keys of a Javascript Object with Object.keys()

Use-Cases of this Tutorial

  • Know how to get all keys of a Javscript object.
  • Know about the Object.keys() method.

Keys of a Javscript object can be retrieved using the Object.keys() method.


The Object.keys() method returns a given object’s own enumerable property names as an array.

  1. An enumerable property of an object is basically “countable”. This property has its “enumerable” attribute set to “true”. If set to “false” it becomes non-enumerable.
  2. Own enumerable properties are those enumerable properties of the object that are owned by the object itself and not inherited through its prototype chain.
See also  3D Text Scrolling Effect In Pure CSS / CSS3

For simple objects we don’t need to worry about enumerable. Basically Object.keys() method returns an array holding all keys of the object.

Example #1 :

var person = {
	name: "John Doe",
	age: 27,
	gender: "M"

var keys = Object.keys(person);

// [ "name", "age", "gender" ]

Example #2 :

// creating objects using constructor function
function Person(name, age, gender) { = name;
	this.age = age;
	this.gender = gender;

Person.prototype = {
	city: "Paris",
	country: "France"

// create new object
var person1 = new Person("John Doe", 27, "M");

var keys = Object.keys(person1);


// [ "name", "age", "gender" ]
// "city" and "country" will not be included as the belong to the prototype chain

Leave a Reply

Your email address will not be published. Required fields are marked *