Why Number(symbol) crashes?

Oriol Bugzilla oriol-bugzilla at hotmail.com
Tue Oct 11 16:32:39 UTC 2016


In my opinion it's a bit incoherent that each object-coercible type behaves differently when coercing symbols to it.

Symbols can be explicitly coerced to strings, but not implicitly:

```js
String(Symbol("abc")); // "Symbol(abc)"
Symbol("abc") + ""; // TypeError
```

Symbols can't be coerced to numbers, neither explicitly nor implicitly:

```js
Number(Symbol("abc")); // TypeError
+Symbol("abc"); // TypeError
```

Symbols can be coerced to booleans, both explicitly and implicitly:

```js
Boolean(Symbol("abc")); // true
!!Symbol("abc"); // true
```

Maybe the `Number` constructor could behave analogous to `String` and return `NaN` for symbols, instead of calling ToNumber.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161011/958ac58d/attachment.html>


More information about the es-discuss mailing list