Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript Object keys() Method

  • 05 Aug, 2025
  • Com 0
JavaScript Object.keys() Method

JavaScript Object.keys() is a built-in static method that returns an array of a given object’s enumerable property names, in the same order as that provided by a for…in loop.

JavaScript Object.keys() Method's Visual Representation

The keys() method only includes properties directly defined on the object, not those inherited from its prototype chain.

let obj = {
  name: 'Krunal',
  education: 'IT Engineer',
  country: 'India'
};

console.log(Object.keys(obj));

// Output: ['name', 'education', 'country']

You can see that the output array contains the keys as elements.

Syntax

Object.keys(obj)

Parameters

Argument Description
obj  It represents an input object whose enumerable properties you want.

Iterating over properties

At first glance, you can assume that it only returns keys, but you can iterate over an object and print its keys and values with the help of the forEach() function.

let student = {
  name: 'Krunal',
  education: 'IT Engineer',
  country: 'India'
};

Object.keys(student).forEach(key => {
  console.log(`${key}: ${student[key]}`);
});

// Output:
// name: Krunal
// education: IT Engineer
// country: India

We displayed Object keys and values as text in the console. You can further process these values based on your requirements. It enables access to Object values.

Working with an Array

Applying JavaScript Object.keys() method to an Array

In JavaScript, arrays are Objects. So, we can apply the Object.keys() method to arrays as well.

Since an array does not have a key like an object does, its indices can be counted as keys.

It will return indices as strings of an array.

// Declaring an array
let arr = [
  'apple',
  'microsoft',
  'amazon',
  'alphabet',
  'tencent',
];

console.log(Object.keys(arr));

// Output: [ '0', '1', '2', '3', '4' ]

Non-Enumerable properties

When creating a new object with properties defined as enumerable: false, the Object.keys() method excludes those properties from the output.

const obj = {};

Object.defineProperty(obj, "hidden", {
  value: "secret",
  enumerable: false
});

obj.visible = "public";

console.log(Object.keys(obj));
// Output: ["visible"]

In the above code, the property hidden does not appear in the final keys array because we set the enumerable to false. The output array only contains one property, “visible”.

Inherited properties

Inherited properties from the prototype chain are not included in the output keys array.

function Student() {
  this.name = "Charlie";
}

Student.prototype.age = 21;

const student = new Student();

console.log(Object.keys(student));

// Output: ['name']

String Object

Strings are iterables and are coerced to String objects, treating each character as a property. That means, it can return the indices of characters as keys.

console.log(Object.keys("hello"));

// Output: [ '0', '1', '2', '3', '4' ]

Empty Objects

When the input Object is empty, the output array won’t include a single element. No enumerable properties result in an empty array.

console.log(Object.keys({}));

// Output: []

Proxy Objects

This method works with proxies, but the behavior depends on the proxy’s trap implementation.

const obj = { kb: 19, kl: 21 };

const proxy = new Proxy(obj, {
  ownKeys() {
    return ["kb"];
  }
});

console.log(Object.keys(proxy));

// Output: ["kb"]

In the above output, the proxy’s ownKeys trap can override the default behavior, affecting the returned keys.

That’s it!

Post Views: 16
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Check If a Variable Exists in Python
How to Remove a Character From String in JavaScript

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend