What are the differences between JavaScript's Object.keys() and Object.values()?
Antek N
antek n profile pic

JavaScript provides two methods,Object.keys() andObject.values(), which allow you to extract information from an object. Here are the differences between these two methods: 1. Returned values: -Object.keys(): - Returns an array of the object's own enumerable property names (keys). -Object.values(): - Returns an array of the object's own enumerable property values. 2. Order of returned values: -Object.keys(): - Returns the property names in the same order as afor...in loop would iterate over the object's properties. -Object.values(): - Returns the property values in the same order asObject.keys(). 3. Usage with arrays and array-like objects: -Object.keys(): - Can be used with arrays or array-like objects to retrieve their indices. - For arrays, it returns an array of string indices. - For array-like objects (e.g., NodeList), it returns an array of indices as strings. -Object.values(): - Does not work directly with arrays or array-like objects. - It throws a TypeError if used on an array or array-like object. 4. Handling inherited properties: -Object.keys() andObject.values(): - Only operate on the object's own enumerable properties and do not include inherited properties. 5. Usage with non-object values: -Object.keys() andObject.values(): - Throw a TypeError if used on non-object values, such asnull orundefined. Here's an example illustrating the differences:

1
2
3
4
5
6
7
8
9

const obj = {
  name: "John",
  age: 30,
  city: "New York",
};

console.log(Object.keys(obj)); // Output: ["name", "age", "city"]
console.log(Object.values(obj)); // Output: ["John", 30, "New York"]

In summary,Object.keys() returns an array of property names, whileObject.values() returns an array of property values. The order of returned values is consistent between the two methods, and they both operate on the object's own enumerable properties. However,Object.keys() can also be used with arrays and array-like objects to retrieve their indices, whereasObject.values() does not work directly with them. Remember to handle non-object values appropriately to avoid TypeError exceptions.

Similar Questions

What are the differences between Object.keys() and Object.values() in JavaScript?

What are the differences between JavaScript's Object.create() and new Object()?

What are the differences between JavaScript's Object.assign() and Object spread?

What are the differences between JavaScript's Object.freeze() and Object.seal() methods?

What are the differences between Object.freeze() and Object.seal() in JavaScript?

What are the differences between JavaScript's Object.create() and Object.assign() methods?

What are the differences between JavaScript's bind(), call(), and apply()?

What are the differences between JavaScript's Promise.resolve() and Promise.reject()?

What are the differences between JavaScript's Array.includes() and Array.indexOf()?

What are the differences between JavaScript's parseFloat() and parseInt()?

What are the differences between Object.assign() and the spread operator in JavaScript?

What are the differences between JavaScript's Math.floor() and Math.ceil()?

What are the differences between map() and forEach() in JavaScript?

What are the differences between JavaScript's String.indexOf() and String.includes()?

What are the differences between JavaScript's push() and concat() methods?

What are the differences between JavaScript's filter() and find() methods?

What are the differences between JavaScript's splice() and slice() methods?

What are the differences between JavaScript's splice() and slice() methods?

What are the differences between JavaScript's for and while loops?

What are the differences between JavaScript's null and undefined?