What is array keys() in JavaScript?

In JavaScript, the keys() method returns an array of the keys used in a given object. This can be helpful for debugging or for understanding how an object is structured.

The keys() method has been available in JavaScript since at least 1995, when it was first introduced in Netscape Navigator 2.0. At that time, the method returned an array of strings representing the keys in the object.

The keys() method was later standardized as part of the ECMAScript language specification, and its return type was changed to an array of strings, numbers, and symbols. In later versions of ECMAScript, the method was also updated to work with arrays and objects of any type.

The keys() method is often used in conjunction with the for...in loop, which can be used to iterate through the keys in an object. The code below shows how to use the keys() method and for...in loop to iterate through the keys in a JavaScript object.

var obj = { firstName: "John", lastName: "Doe", age: 34 };
var keys = obj.keys();

for (var key in keys) {
	console.log("Key: " + key + " Value: " + obj[key]);

The output of the code above will be:

Key: firstName Value: John
Key: lastName Value: Doe
Key: age Value: 34
Want to sponsor matcha.fyi? You can have your brand featured right here. Let's connect and chat about it.