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
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.
🌐
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.
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
How to use JavaScript Object.keys() - Keys Explained with Examples
The Object.keys() method returns an array of a given object’s own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototy… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
September 8, 2019
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
February 4, 2022
🌐
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
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Working_with_objects
Working with objects - JavaScript | MDN
February 21, 2026 - 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.
🌐
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 […]
Find elsewhere
🌐
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.
🌐
DEV Community
dev.to › ritam369 › understanding-objects-in-javascript-fh8
Understanding Objects in JavaScript - DEV Community
1 week ago - That’s exactly what a JavaScript Object does – it stores related information in clean key-value pairs, just like a bio-data!
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Keyed_collections
Keyed collections - JavaScript | MDN
A WeakMap is a collection of key/value pairs whose keys must be objects or non-registered symbols, with values of any arbitrary JavaScript type, and which does not create strong references to its keys. That is, an object's presence as a key in a WeakMap does not prevent the object from being ...
🌐
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.
🌐
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.
🌐
GitConnected
levelup.gitconnected.com › learn-about-object-keys-in-javascript-cf2967ba6401
How to Use Object.keys in JavaScript
November 1, 2019 - The Object.keys() method returns an array of strings of a given object's own property/key names.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › keys
Set.prototype.keys() - JavaScript | MDN
July 10, 2025 - The keys() method of Set instances is an alias for the values() method. ... A new iterable iterator object.
🌐
Mohit Designs
mohitdesigns.com › home › javascript › understanding javascript objects: key concepts and examples
JavaScript Objects: Key Concepts, Properties, and Examples
September 21, 2024 - JavaScript provides built-in methods like Object.keys(), Object.values(), and Object.entries() to manipulate objects.
🌐
Luis Llamas
luisllamas.es › inicio › cursos › curso javascript
Keys, Values, and Entries Methods in JavaScript
December 6, 2024 - The Object.keys() method returns an array with the names of the enumerable properties of an object.
🌐
Scaler
scaler.com › home › topics › object keys javascript
JavaScript Object.keys( ) Function - Scaler Topics
May 4, 2023 - The Object keys javascript function is used for returning an array, the elements of which are strings corresponding to the enumerable properties found directly upon an object. The ordering of these properties is the same as the order obtained ...
🌐
Quora
quora.com › How-can-you-use-the-Object-keys-Object-values-and-Object-entries-methods-to-access-the-properties-and-values-of-an-object-in-JavaScript
How to use the Object.keys, Object.values, and Object.entries methods to access the properties and values of an object in JavaScript - Quora
Answer: [code]const myObject = { a: 1, b: 2, c: 3 }; // Using Object.keys to get property names const keys = Object.keys(myObject); console.log(keys); // Output: ['a', 'b', 'c'] // Using Object.values to get property values const values = Object.values(myObject); console.log(values); // Outpu...
🌐
freeCodeCamp
forum.freecodecamp.org › guide
How to use JavaScript Object.keys() - Keys Explained with Examples
September 8, 2019 - The Object.keys() method returns an array of a given object’s own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well). Syntax Object.keys(obj) Parameters obj The object whose enumerable own properties are to be returned.
🌐
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!