What is the type of "keys" in JavaScript?

tunylund picture tunylund · Aug 31, 2010 · Viewed 19k times · Source

I bumbed into one of those moments when I just lose the focus and start wondering on a silly question:

var a = {
  b: "value"
}

What is the typeof 'b' and I don't mean the typeof "value", but the actual Key labeled as b?

background: I started wondering about this when I had to create a key which is a string:

var a = {
  "b": "value"
}

because at a later point it is referenced as:

a["b"]

And then eneded up wondering the original question.

Answer

Andy E picture Andy E · Aug 31, 2010

In object literal terms, b is a property. Properties are either strings or symbols in JavaScript, although when defining the property name inside an object literal you may omit the string delimiters.

for (key in a) {
    alert(typeof key);
    //-> "string"
}