How to get First Property of an object in JavaScript

Javascript get first property of object example; In this tutorial you will learn how to access the First Property of an Object in JavaScript.

In javascript we can use the Object.values and Object.keys to get the first elemenet from an object.

Example 1:

The Object.values method returns an array containing all of the object’s values. Here getting the the first property of an object you need to pass the object to the Object.values method and access the element at index 0.

// Supported in IE 9-11
const obj = {first: '1', second: '2', third: '3'};

const keys = Object.keys(obj);
console.log(keys)  // ['one', 'two', 'three']

const firstValue = obj[Object.keys(obj)[0]]; // '1'

Example 2:

You can use the Object.keys method, which is supported in Internet Explorer 9-11.

// Not Supported in IE 6-11
const obj = {first: '1', second: '2', third: '3'};

const values = Object.keys(obj);
console.log(values)  // ['1', '2', '3']

const firstValue = Object.values(obj)[0]; 
console.log(firstValue); // '1'