Why is BigInt not a constructor function, unlike Number, Boolean and String?

Number, Boolean and String are constructor functions that correspond to those primitive types.

BigInt is a function that corresponds to the BigInt primitive type, but it is not a constructor function.

new BigInt(1n) // Uncaught TypeError: BigInt is not a constructor

Why?

>Solution :

The functions for new primitive types (Symbol, BigInt) don’t get construction signatures because TC39 doesn’t think they need them. On this issue on the BigInt proposal, Jordan Harband (TC39 member and former editor of the specification) said:

Just like Symbol, it doesn’t make sense to use new with things that produce a primitive. Since BigInt (like Symbol) is a primitive, it should only be invoked as a function.

Pre-ES6 primitive constructors unfortunately must retain their new-ability, for back compat.

and from this one (also Jordan):

…users should never use new with a Number object tho, because it’s widely considered a very bad practice to ever use boxed primitives.

The new paradigm (no pun intended) is the one Symbol follows – if you want an object, you have to explicitly pass the primitive into Object. new Primitive() is a footgun, and new primitives should not continue this legacy pattern.

The use cases for new String, etc., are very few and far between (if there even are any that aren’t better solved in other ways). Having construction signatures for Number and String and Boolean is confusing (do you create a string just by using a literal, or by using new String? why is it new String("x") === "x" is false? etc.). It looks like the committee decided to avoid that kind of confusion with newer primitives. (They still have object counterparts, it’s just slightly less obvious how you get them, making it less likely to cause confusion.)

Leave a Reply