Is there ever a situation where we want to create a number using `new Number()`?

Referring to this:
Number() constructor - JavaScript | MDN (
Would appreciate an example.

Similarly with strings where it reads “you rarely want to use the String constructor at all”:
String() constructor - JavaScript | MDN (
So is it “rarely” or “at all”?

Strings (or numbers, or booleans etc) are primitives.

When you apply a method to them, like "hello".toUpperCase(), JS puts the string in an object (it “boxes” it), applies the method, then returns a new primitive string.

If you want that object, then that’s what new String is creating. If you want direct access to the underlying way JS handles the built-in objects it uses to do everything, then you might use the constructors. They are available, it’s just very unlikely you will be in a position where you need them.



This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.