views:

47

answers:

3

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.

+2  A: 

b is a string, it's just a shorthand syntax, so you write

var a = {
    b: "value"
}

instead of

var a = {
  "b": "value"
}
aularon
+3  A: 

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

When looping through property names using a for...in, the property name is a string:

for (key in a) {
    alert(typeof key);
    //-> "string"
}
Andy E
Well now i've started wondering if 'key' is a string only when you loop the keys in a? - but I guess that depends on the implementation of the javascript engine.
tunylund
@tunylund: No key is a string always. I gave some examples in my answer below :)
Daniel Vassallo
@tunylund: As @Daniel said, the key is always a string. Since there's no real use case for testing the type of an object's key (not the key's value), and there's no way to do it outside of enumerating the keys with a loop like this, it's not something you should be worrying about :)
Andy E
True enough, but it helps in understanding the language as a whole to know details like this.
tunylund
+1  A: 

Keep in mind that JavaScript objects are hash tables and the keys are just strings. You may omit the quotes around property names during declaration, but if you use reserved words for property names or any other name that happens to be an invalid identifier, such as starting with a digit, or containing spaces, you would have to wrap the property names in quotes:

var a = {
  "1b":       "value",
  "b and c":  "value",
  "+12345":   "value"
};

Also note that you can reference the properties of objects using the dot notation or the subscript notation regardless of whether quotes where used when they were declared. However, if you use property names that would be invalid identifiers, such as the ones in the above example, you are forced to use the subscript notation:

a.1b             // invalid (dot notation)
a["b and c"];    // valid   (subscript notation)
Daniel Vassallo
Do those invalid identifiers work in all (major) browsers? Don't you risk throwing an exception in some JavaScript engines?
Marcel Korpel
@Marcel: subscript notation is defined by the ECMA-262 specification and any string is valid.
Andy E
@Marcel: Yes, they should work in all browsers. Objects are hash tables, and accept any string as a key (property name). Douglas Crockford describes the topic here (section Objects): http://www.crockford.com/javascript/survey.html
Daniel Vassallo