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?

6
  • Both lines implicitly convert the number to a string. typeof Object.keys(obj)[0] === "string" Commented May 4, 2017 at 9:13
  • What exactly is your question? Commented May 4, 2017 at 9:19
  • You can access it using obj["2.15"], it's supposed to be supported by all browsers Commented May 4, 2017 at 9:20
  • @torazaburo Do you know where I might find the official specification to whether or not I can use decimal fraction? Commented May 4, 2017 at 9:24
  • @Vivick I know I can use the string form, but it looks awkward Commented May 4, 2017 at 9:26

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.