We will discuss two implementations to achieve this task. Why? Because at the time of writing this article in 2022, browser backward compatibility is still a thing.

Method 1 - using Object.value()

For accessing the first property of an object, we will use the "Object.value()" method by passing the object as an argument to it. For eg:

const object = { 1: "first", 2: "second", 3: "third" };

const result = Object.values(object);
console.log(result);  //[ 'first', 'second', 'third' ]  πŸ‘ˆ

From here, we can simply access the desired element of the returned result. i.e.


const firstProperty = result[0]; 
console.log(firstProperty) //first  πŸ‘ˆ

or combined in a single line as

const object = { 1: "first", 2: "second", 3: "third" };

const result = Object.values(object)[0];
console.log(result);  //first  πŸ‘ˆ


However, Internet Explorer 6 to 11, which nobody uses anyhow, doesn't support "Object.value()" method. The sad reality of a developer!. However, you could solve this compatibility issue by using a javascript compiler like Babel or by using a polyfill. Now, let me introduce you to a different method that doesn't involve any of the fancy tools or packages I just mentioned.

Method 2 - using Object.keys()

"Object.keys()" method is supported in every browser. Some of you might have already guessed how this implementation is going to be done. "Object.keys()" method selects the keys of an object and returns them in an array. For eg:

const object = { 1: "first", 2: "second", 3: "third" };

const result = Object.keys(object);
console.log(result);  //[ '1', '2', '3' ]  πŸ‘ˆ

Now, we can simply use the key to access the required element in the object:

const firstProperty = object[result[0]];
console.log(firstProperty);  //first  πŸ‘ˆ

Which method should you use?

The second method of implementation requires two steps as I explained. This is only done to achieve browser backward compatibility and the choice depends entirely upon your application requirements. As we move forward with technology, supporting old browsers will be difficult and these older versions of browsers pose several security risks to the users.

Thank you for reading. Β Feel free to check other articles in my blog that you find interesting.