Description
Where
Chapter 6 - Symbols, under Creating Symbols
Problem
Because symbols are primitive values, calling
new Symbol()
throws an error when called.
Reason
The cause of the error is not due to symbols being primitive values. Otherwise calling
new Boolean();
or
new Number();
would also throw an exception.
I did not find any record explaining the reason for this specification for throwing a TypeError when trying to explicitly call the Symbol constructor to get a Symbol object, but I believe it goes along the same lines as throwing a TypeError when trying to coerce a symbol implicitly to a string or number
const sym = Symbol();
'' + sym // TypeError
2 * sym // TypeError
The specification goes a long way towards making sure that symbols remain symbols and to prevent using them as a different type by accident.
According to Axel Rauschmayer the reason is most likely preventing accidental and unintentional type conversions which would lead to errors, mainly due to the role symbols play as unique property keys.
https://exploringjs.com/es6/ch_symbols.html#sec_converting-symbols-to-primitives
Solution 1
calling
new Symbol()
throws an error when called.
Solution 2
In order to prevent creating symbol objects instead of symbol primitive values, calling
new Symbol()
throws an error when called.