3 ms·
It's consistent with the other primitive functions: console.log(Boolean(5)); // true console.log(Number("5")); // 5 console.log(String(1)+1); // "1
by AgentME 11y ago
It's consistent with the other primitive functions:
console.log(Boolean(5)); // true
console.log(Number("5")); // 5
console.log(String(1)+1); // "11"
Using new with any of the other primitive functions takes the return value and creates it into an object with the primitive function's prototype, which is kind of a coincidence it looks like it does anything useful but can cause a lot of issues because primitives and objects are different. If backwards compatibility weren't king, I'd expect to see that get removed. Instead, the best they can do is make any new primitive functions throw a TypeError if used with `new`.
- draw_down 11y agoIt's not consistent, calling those functions with `new` returns an object representation of the primitive value (you can get the primitive value by calling .valueOf or by coercing). It does not cause an error.
- AgentME 11y agoThe correct way of using it is consistent. I'd be surprised if being able to use `new` with the other primitive functions was ever specifically intended and if that choice to allow it wasn't regretted. It's a common source of confusion and is not useful.