Home

Published

- 1 min read

how to get the first key in a n object in javascript

img of how to get the first key in a n object in javascript

The solution for this is noted below

how to get the first key in a n object in javascript

Solution

   var obj = { a: 1, b: 2, c: 3 }
alert(Object.keys(obj)[0])
// alerts "a"
let objLength = Object.keys(obj).length
console.log(objLength)
// Logs 3 as "obj" has 3 keys
var objValue = Object.values(obj)[0]
console.log(objValue)
// Logs "1" as the value of the first key(a) is "1"

Try other methods by searching on the site. That is if this doesn’t work