1

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?

Community
  • 1
  • 1
garajo
  • 736
  • 4
  • 19
  • Both lines implicitly convert the number to a string. `typeof Object.keys(obj)[0] === "string"` – JJJ May 04 '17 at 09:13
  • What exactly is your question? –  May 04 '17 at 09:19
  • You can access it using obj["2.15"], it's supposed to be supported by all browsers – Vivick May 04 '17 at 09:20
  • @torazaburo Do you know where I might find the official specification to whether or not I can use decimal fraction? – garajo May 04 '17 at 09:24
  • @Vivick I know I can use the string form, but it looks awkward – garajo May 04 '17 at 09:26
  • Using a floating point was a bit weird but if it's correct according to the specification then go ahead, less "number to string" conversion is always nice – Vivick May 04 '17 at 09:39

1 Answers1

1

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335