function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key] === value);
}

ES6, no prototype mutations or external libraries.

Example,

function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key] === value);
}


const map = {"first" : "1", "second" : "2"};
console.log(getKeyByValue(map,"2"));

Answer from UncleLaz on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › keys
Object.keys() - JavaScript - MDN Web Docs
Object.keys() returns an array whose elements are strings corresponding to the enumerable string-keyed property names found directly upon object. This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well.
🌐
Tabnine
tabnine.com › home › how to get an object’s keys and values in javascript
How to Get an Object’s Keys and Values in JavaScript - Tabnine
July 25, 2024 - In JavaScript, getting the keys and values that comprise an object is very easy. You can retrieve each object’s keys, values, or both combined into an array. The examples below use the following object: const obj = { name: 'Daniel', age: 40, occupation: 'Engineer', level: 4 }; Getting an object’s keys The Object.keys() method returns […]
Discussions

How to get a key in a JavaScript object by its value? - Stack Overflow
I have a quite simple JavaScript object, which I use as an associative array. Is there a simple function allowing me to get the key for a value, or do I have to iterate the object and find it out More on stackoverflow.com
🌐 stackoverflow.com
Object.keys
obj is an object with the keys "username1", "username2", and "username3", and the respective values of "ben", "kevin", and "deku". The operation Object.keys(obj) creates and returns a new array object that has all the keys of obj. That array would look like ["username1", "username2", "username3"] where "username1" has an index of 0, "username2" has an index of 1, and "username3" has an index of 2. When you call forEach on this array, the first two arguments in the callback function is going to be the value of the element in that iteration of the array ("username1", "username2", or "username3") and the index of the element in that iteration (0, 1, and 2, respectively). Ultimately you're working with two objects, obj which has key-value pairs, and the keys array from Object.keys(obj) which has indexed values matching the keys of obj. More on reddit.com
🌐 r/learnjavascript
21
1
June 26, 2024
Properly type Object.keys and Object.entries
Unfortunately, there's not a very strong parallel between Object.keys and keyof since Object.keys only returns string keys. This has always been a pain point in TS (hopefully we get some form of exact types soon), but in the meantime, here's what I use for narrowed Object.keys: /** Mimics the result of Object.keys(...) */ export type keysOf = o extends readonly unknown[] ? number extends o["length"] ? `${number}` : keyof o & `${number}` : { [K in keyof o]: K extends string ? K : K extends number ? `${K}` : never }[keyof o] export const keysOf = (o: o) => Object.keys(o) as keysOf[] More on reddit.com
🌐 r/typescript
5
6
August 7, 2023
Help writing a type for object with dynamic keys.
Your type could be defined as either a dictionary (look up by "any" string type) or an object with set properties -- not both. While there may be wonky ways to get around it, it's not worth it and your best bet is to nest the record type to a property (also the recommended way of doing things) type normalizedUsers = { byId: Record; allIds: string[]; } More on reddit.com
🌐 r/typescript
4
4
December 6, 2021
🌐
W3Schools
w3schools.com › jsref › jsref_object_keys.asp
JavaScript Object.keys() Method
The Object.keys() method returns an array with the keys of an object.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript - MDN Web Docs - Mozilla
1 month ago - In the above code, the key anotherObj is an object, which is neither a string nor a symbol. When it is added to the myObj, JavaScript calls the toString() method of anotherObj, and use the resulting string as the new key.
🌐
Educative
educative.io › answers › how-to-get-keys-values-and-entries-in-javascript-object
How to get Keys, Values, and Entries in JavaScript Object?
It prints the object’s properties and values to the console. To access a specific key-value pair in a JavaScript object, we can use Object.entries() to convert a JavaScript object into an array of key-value pairs and then filter this array to retrieve the value associated with a specific key.
🌐
Codecademy
codecademy.com › docs › javascript › objects › .keys()
JavaScript | Objects | .keys() | Codecademy
July 31, 2025 - The Object.keys() JavaScript method returns an array of a given object’s own enumerable string-keyed property names.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Object › keys
JavaScript Object keys() - Get Object Keys | Vultr Docs
November 6, 2024 - The Object.keys() method in JavaScript is a handy tool for extracting the keys from an object.
Find elsewhere
🌐
Programiz
programiz.com › javascript › library › object › keys
JavaScript Object.keys()
Become a certified JavaScript programmer. Try Programiz PRO! ... The Object.keys() method returns an array of a given object's own enumerable property names.
🌐
Mimo
mimo.org › glossary › javascript › object-keys-method
JavaScript Object.keys() method: Syntax, Usage, and Examples
The Object.keys() method in JavaScript returns an array of a given object’s own enumerable property names. It’s a useful tool for iterating over keys, checking what data an object holds, or transforming objects into arrays.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › entries
Object.entries() - JavaScript - MDN Web Docs
Each key-value pair is an array with two elements: the first element is the property key (which is always a string), and the second element is the property value. Object.entries() returns an array whose elements are arrays corresponding to the enumerable string-keyed property key-value pairs found directly upon object.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › values
Object.values() - JavaScript - MDN Web Docs
Object.values() returns an array whose elements are values of enumerable string-keyed properties found directly upon object. This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well.
🌐
Medium
medium.com › @conboys111 › how-do-object-keys-object-values-and-object-entries-differ-in-javascript-8d1c19901ecb
How Do Object.keys(), Object.values(), and Object.entries() Differ in JavaScript? | by myHotTake | Medium
September 27, 2024 - With Object.keys() in hand, I approached my first exhibit. This map only showed the names of the treasures — nothing about their value, just the labels. Imagine walking through a hallway of artifacts, each with a tag underneath it. This map said, “Read the names of the treasures in the order they were added to the collection, but wait!
🌐
Dmitri Pavlutin
dmitripavlutin.com › access-object-keys-values-entries
How to Access Object's Keys, Values, and Entries in JavaScript
August 11, 2020 - Object.keys(hero) returns the list ['name', 'city'], which, as expected, are the keys of hero object.
🌐
Reddit
reddit.com › r/learnjavascript › object.keys
r/learnjavascript on Reddit: Object.keys
June 26, 2024 -

Do you use Object.keys often?

Can you explain what's going on in this code what would be the key, index.

At first I was still thing they was object not arrays because that's what they looked like. I know the key was username1/2/3/ and the value is ben/kevin/deku but they don't have index and removing index from the parameters changed nothing, but arrays have index but not keys. from the cosole.log to get the key we console.log key, and to get the value its obj[key] but why?

I tried console.log(Object.keys(obj)) but that mad it more confusing because now the names(values) where gone and it was just an array of the username(keys)

let obj ={
    username1: "ben",
    username2: "kevin",
    username3: "deku"
}
Object.keys(obj).forEach((key, index) =>{
    console.log(key, obj[key]);
})
// username1 ben
// username2 kevin
// username3 deku



let obj ={
    username1: "ben",
    username2: "kevin",
    username3: "deku"
}
Object.keys(obj).forEach((key) =>{
    console.log(key, obj[key]);
})
// username1 ben
// username2 kevin
// username3 deku



let obj ={
    username1: "ben",
    username2: "kevin",
    username3: "deku"
}
Object.keys(obj).forEach((key, index) =>{
    console.log(obj[key]);
})
// ben
// kevin
// deku



let obj ={
    username1: "ben",
    username2: "kevin",
    username3: "deku"
}
Object.keys(obj).forEach((key) =>{
    console.log(key);
})
// username1
// username2
// username3
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-object-keys-method
JavaScript Object keys() Method - GeeksforGeeks
The Object.keys() method in JavaScript is used to retrieve an array of the enumerable property names of an object.
Published   July 12, 2024
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › keys
Array.prototype.keys() - JavaScript - MDN Web Docs
July 20, 2025 - The keys() method is generic. It only expects the this value to have a length property and integer-keyed properties. Unlike Object.keys(), which only includes keys that actually exist in the array, the keys() iterator doesn't ignore holes representing missing properties.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › object › transform object keys
Transform the keys of a JavaScript object - 30 seconds of code
February 19, 2024 - A very common key transformation is to convert all the keys of an object to upper or lower case. In the previous article, can find a more detailed explanation of how to uppercase or lowercase object keys in JavaScript.
🌐
Sentry
sentry.io › sentry answers › javascript › how can i add a key-value pair to a javascript object?
How Can I Add a Key-Value Pair to a JavaScript Object? | Sentry
The key should be a String or a symbol. If you need a key to be a different primitive value or an object, or if the insertion order of the key-value pairs is important, don’t use a regular JavaScript object.