How to Get a List of Array Keys in JavaScript? Last Updated : 15 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Here are the different methods to get a list of associative array keys in JavaScript1. Using JavaScript for each loopIn this method, traverse the entire associative array using a for each loop and display the key elements of the array. javascript //Driver Code Starts let a = {Newton: "Gravity",Albert: "Energy",Edison: "Bulb",Tesla: "AC"}; //Driver Code Ends for (let key in a) { if (a.hasOwnProperty(key)) { console.log(key); } } OutputNewton Albert Edison Tesla In this exampleIt checks if each key belongs directly to arr (not inherited) with hasOwnProperty().Valid keys (Newton, Albert, Edison, Tesla) are logged to the console.2. Using Object.keys() methodThe Object.keys() is an inbuilt function in JavaScript that can be used to get all the keys of the array. javascript let arr = {Newton: "Gravity",Albert: "Energy",Edison: "Bulb",Tesla: "AC"}; let keys = Object.keys(arr); console.log(keys); Output[ 'Newton', 'Albert', 'Edison', 'Tesla' ] In this exampleThe code retrieves the keys of the associative array arr using Object.keys(), which returns an array of the keys. It then logs the keys to the console.3. Using Object.getOwnPropertyNames() methodThe Object.getOwnPropertyNames() method in JavaScript is a standard built-in object which returns all properties that are present in a given object except for those symbol-based non-enumerable properties. JavaScript let a = {Newton: "Gravity",Albert: "Energy",Edison: "Bulb",Tesla: "AC"}; const keys = Object.getOwnPropertyNames(a); console.log(keys); Output[ 'Newton', 'Albert', 'Edison', 'Tesla' ] In this exampleThe code retrieves and prints the keys (property names) of the arr object using Object.getOwnPropertyNames().4. Using Object.entries() methodJavaScript Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. JavaScript let a = { name: 'Jaya', age: 25, city: 'New York' }; let keys = Object.entries(a).map(([key, _]) => key); console.log(keys); Output[ 'name', 'age', 'city' ] In this exampleThe code retrieves and prints the keys of the a object by using Object.entries() and map().It converts the object's entries into an array of keys and then logs them.5. Using Reflect.ownKeys() methodJavaScript Reflect.ownKeys() method in Javascript is used to return an array of the target object’s own property keys and it ignores the inherited properties. JavaScript let a = { name: 'Jaya', age: 25, city: 'New York' }; let keys = Reflect.ownKeys(a); console.log(keys); OutputKeys are listed below [ 'name', 'age', 'city' ] In this exampleThe code uses Reflect.ownKeys() to retrieve all the keys (including non-enumerable ones) of the a object and logs them.6. Using Object.getOwnPropertySymbols() methodIn JavaScript, the Object.getOwnPropertySymbols() method returns an array of all symbol properties found directly upon a given object. JavaScript let s1 = Symbol('a'); let s2 = Symbol.for('b'); let obj = { [s1]: 'hello', [s2]: 'world' }; let symbols = Object.getOwnPropertySymbols(obj); console.log(symbols); Output[ Symbol(a), Symbol(b) ] In this exampleThe code retrieves and logs the symbol keys of the obj object using Object.getOwnPropertySymbols(). Comment More infoAdvertise with us Next Article How to Get a List of Array Keys in JavaScript? R Rajnis09 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA Similar Reads How to store a key=> value array in JavaScript ? In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value. This allows you to store and retrieve data using named keys instead of numeric indices, enabling more intuitive access to the stored information.Here are some common approaches:Table 4 min read How To Convert Map Keys to an Array in JavaScript? Here are the various methods to convert Map keys to an array in JavaScript1. Using array.from() MethodThe Array.from() method in JavaScript converts Map keys to an array by using 'Array.from(map.keys())'. JavaScriptlet map = new Map().set('GFG', 1).set('Geeks', 2); let a = Array.from(map.keys()); co 2 min read How to Convert Object to Array in JavaScript? In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such 4 min read How to Access Array of Objects in JavaScript ? Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How 4 min read How to convert a map to array of objects in JavaScript? A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods 6 min read How to get Array Structure with alert() in JavaScript? To display an array structure using alert() in JavaScript, the array can be converted into a string format Array.toString(). This allows the contents of the array to be shown in a readable format within an alert box.Below are the approaches to get array structure with alert() in JavaScript: Table of 2 min read How to get the Value by a Key in JavaScript Map? JavaScript Map is a powerful data structure that provides a convenient way to store key-value pairs and retrieve values based on keys. This can be especially useful when we need to associate specific data with unique identifiers or keys.Different Approaches to Get the Value by a Key in JavaScript Ma 3 min read How to Convert String of Objects to Array in JavaScript ? This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i 3 min read How to filter Nested Array using Key in JavaScript ? Filtering a nested array in JavaScript involves the process of selectively extracting elements from an array of objects, where each object contains another array. We will discuss how can we filter Nested Array using the key in JavaScript. Filtering a nested array based on a key in JavaScript can be 3 min read How to Convert an Array of Objects to Map in JavaScript? Here are the different methods to convert an array of objects into a Map in JavaScript1. Using new Map() ConstructorThe Map constructor can directly create a Map from an array of key-value pairs. If each object in the array has a specific key-value structure, you can map it accordingly.JavaScriptcon 3 min read Like