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
The Object.keys() static method returns an array of a given object's own enumerable string-keyed property names.
🌐
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.
🌐
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.
🌐
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.
🌐
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 - The Object.keys() method returns an array of strings containing all of the object’s keys, sorted by order of appearance:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-object-keys-and-object-entries-methods-in-javascript
Difference between Object.keys() and Object.entries ...
July 12, 2025 - Object.keys() iterates over an object's own enumerable properties, returning an array containing the keys of those properties.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-object-keys-tutorial-how-to-use-a-js-key-value-pair
JavaScript Object Keys Tutorial – How to Use a JS Key-Value Pair
November 11, 2020 - You can group related data together into a single data structure by using a JavaScript object, like this: const desk = { height: "4 feet", weight: "30 pounds", color: "brown", material: "wood", }; An object contains properties, or key-value pairs. The desk object above has four properties.
🌐
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.
🌐
Medium
medium.com › @lainakarosic › using-object-keys-to-turn-javascript-objects-into-array-f248dc716321
Using Object.keys() to Turn Javascript Objects into Array | by Laina Karosic | Medium
November 25, 2019 - Voila! Here we have a nice return of the keys of our newly created array. Object.keys() essentially extracts all the keys and returns a new array.
🌐
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 ...