garajo
garajo

Reputation: 756

javascript object property name as decimal notation

This is a more specific to this question, which only goes as far as discussing keys as numeric whole integers.

In regards to 'numeric or string literal for the name of a property', I've tried this in the Chrome console var obj = { 2.15: 'foo' } console.log(obj[2.15]) and it works. I would not assume it standard across all browsers, especially older ones. However such a key notation as part of a default configuration provided by my users could be useful. I think using Map, though within standards ('Any value (both objects and primitive values) may be used as either a key or a value'), could be intimidating. If it works, why should it not be used?

Upvotes: 1

Views: 850

Answers (1)

Quentin
Quentin

Reputation: 944013

The specification says:

PropertyName :
  IdentifierName
  StringLiteral
  NumericLiteral

and shows that floats are fine in a NumericLiteral.

It's standard. Use it if you want to.

Upvotes: 2

Related Questions