36

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 ended up wondering about the original question.

Đạt Phạm
  • 13
  • 1
  • 7
tunylund
  • 371
  • 1
  • 3
  • 4

5 Answers5

35

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"
}
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • 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 Aug 31 '10 at 11:11
  • @tunylund: No key is a string always. I gave some examples in my answer below :) – Daniel Vassallo Aug 31 '10 at 11:16
  • 1
    @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 Aug 31 '10 at 11:23
  • 1
    True enough, but it helps in understanding the language as a whole to know details like this. – tunylund Aug 31 '10 at 12:07
  • I believe that a JS property can also be a Symbol, although perhaps toString() is called on the Symbol under the hood. – Alexander Mills Nov 15 '17 at 18:44
  • Symbols have properties that would prevent them from being coerced to String. E.g. both of these statements are true: `Symbol('test') != Symbol('test')` and `Symbol('test') != 'test'` – hiljusti Nov 27 '18 at 19:51
  • @JustinHill, thanks - I've updated the answer. It's worth mentioning that my answer did predate Symbols by almost 5 years ;-) If you come across an answer that may have changed in the future, you can suggest an edit that will be peer reviewed. – Andy E Nov 28 '18 at 12:42
  • You will never get a `Symbol` using for in. It doesn't include symbol keys. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in to quote the mozilla doc: "The for...in statement iterates over all non-Symbol, enumerable properties of an object." – Ozymandias Jul 22 '19 at 04:58
27

Property names are automatically coerced into a string. You can try this yourself by using a numeric literal as a property name.

var object = {
  .12e3: 'wut'
};
object[.12e3]; // 'wut'
object['.12e3']; // undefined
object['120']; // 'wut'

// Let’s try another numeric literal:
object = {
  12e34: 'heh'
};
object[12e34]; // 'heh'
object['12e34']; // undefined
object[1.2e35]; // 'heh'
object['1.2e35']; // undefined
object[1.2e+35]; // 'heh'
object['1.2e+35']; // 'heh'

For this reason, I’d recommend using only string literals for property names.

From Unquoted property names / object keys in JavaScript, my write-up on the subject:

Quotes can only be omitted if the property name is a numeric literal or a valid identifier name.

[…]

Bracket notation can safely be used for all property names.

[…]

Dot notation can only be used when the property name is a valid identifier name.

I also made a tool that will tell you if any given property name can be used without quotes and/or with dot notation. Try it at mothereff.in/js-properties.

Screenshot

Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
  • 1
    I think it's worth mentioning that one can use **numberic literal** as a key with bracket notation as you describe. But with letters, it's evalutated as a variable. `object[x]` is not `object['x']`. – Rick Aug 30 '20 at 15:56
5

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

var a = {
    b: "value"
}

instead of

var a = {
  "b": "value"
}
aularon
  • 11,042
  • 3
  • 36
  • 41
5

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 were 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)
Rick
  • 7,007
  • 2
  • 49
  • 79
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • Do those invalid identifiers work in all (major) browsers? Don't you risk throwing an exception in some JavaScript engines? – Marcel Korpel Aug 31 '10 at 11:06
  • 1
    @Marcel: subscript notation is defined by the ECMA-262 specification and any string is valid. – Andy E Aug 31 '10 at 11:08
  • @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 Aug 31 '10 at 11:11
-1

var a = {$ : 'hello', 2123 : 'number'};
for(var key in a) {
  console.log(typeof key)
}

Keys in javascript objects can be strings and symbols. symbol is a primitive data type in javascript.