Saturday 22 December 2018

What is the type of object keys in JavaScript?

In JavaScript, an object treats each key as a string whether it's a number value, boolean value or any other primitive value.

HelloWorld.js
var obj = {"id" : 123, 1 : "2", 2 : "3", 3 : "5", true : true};

for(var key in obj){
  console.log(key + " : " + typeof key)
}

Output
1 : string
2 : string
3 : string
id : string
true : string

Previous                                                 Next                                                 Home

No comments:

Post a Comment