Why Number(symbol) crashes?
claude.pache at gmail.com
Fri Oct 14 18:02:38 UTC 2016
> Le 11 oct. 2016 à 11:07, medikoo <medikoo+mozilla.org at medikoo.com> a écrit :
> I was searching the archived but wasn't able to find the answer.
> What's the reasoning behind having Number(symbol) crash instead of returning
> NaN (as it's in case all other non-coercible values?). It feels not
> If someone can point me to some discussion that provided the reasoning I'd
> be grateful
I believe that the reason of the inconsistency is more an accident of history in the development of ES6 than a well-reasoned design decision. Here is my understanding:
* At one point of time in the development of ES6, /implicit/ conversion from symbol to string or number was made forbidden. This is a behaviour that was discussed and decided, in order to prevent silent bugs in computed properties in situations where a string was previously implicitely expected.
var key = Symbol()
foo[‘key + ‘-suffix’] // TypeError: can’t convert symbol to string
foo[‘key + ‘42] // TypeError: can’t convert symbol to number
* However, /explicit/ conversion from symbol to string (using `String(…)` or `.toString()`) has been kept as allowed. This is a more debatable decision (with pros and cons). On the other hand, I don’t recall any discussion about making explicit conversion from symbol to number different from the implicit one, nor anyone complaining that it remained the only primitive type to which symbol could not be *explicitly* coerced.
More information about the es-discuss