JavaScript logoJavaScript/
JS-0233

Found new operators with the `Symbol` objectJS-0233

Major severityMajor
Bug Risk categoryBug Risk

The global variable Symbol is a function, and should be called as a constructor with the new operator.

The Symbol() function returns a value of type symbol and has:

  • static properties that expose several members of built-in objects
  • static methods that expose the global symbol registry, and resembles a built-in object class

But it is incomplete as a constructor and hence does not support the syntax new Symbol(). Every symbol value returned from Symbol() is unique. A symbol value may be used as an identifier for object properties. This is the data type's primary purpose, although other use-cases exist, such as enabling opaque data types, or serving as an implementation-supported unique identifier in general.

const foo = new Symbol("foo");

This throws a TypeError.

Bad Practice

const foo = new Symbol('foo');
const foo = Symbol('foo');
class Symbol { /* ... */ }
const symbol = new Symbol("baz");