1

Are there any good reasons to use new Number(n)? Why does this constructor exist?

I can think of several reasons to avoid it, eg.,

  • literals are more memory efficient since they don't construct anything new.
  • literals are equal to each other (5 === 5 but new Number(5) !== new Number(5)).
  • the typeof operator indicates that it's a number rather than an object.
  • new Number(0) is never falsey. (!!new Number(false) == true).
  • You can use it for type conversion, but there are more concise methods. (+false === 0).

To be clear, I understand the Number object has useful methods and properties, my question is specifically about the constructor.

I'm using Number as an example, but I don't understand why Javascript exposes other constructors where literals are available either (String, Object, Array, etc.).

Why do these exist and are there ever any good reason to use them?

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • 2
    The Number constructor is called implicitly in cases like `(1.234).toFixed(1)` (which is nonsensical). The semantics of the language require object "partner" types for the primitive types. In practical terms, explicit deliberate use of Number objects in real code is what I would consider a terrible design decision, and is probably extremely rare. – Pointy Jul 15 '20 at 12:41
  • See https://stackoverflow.com/a/11988751/841830 I used `Number(s)`, in preference to `parseFloat()`, for this very reason the other day. (Also note the difference between `new Number(s)` and `Number(s)` ) – Darren Cook Jul 15 '20 at 12:46
  • @deceze - The threads you posted are not duplicates. They are relevant but I already know that stuff and stated that in my question. Please reopen the question unless you can find one that answer the questions ***Why do the constructors exist, and is there any reason to use them***. This is not a duplicate. I searched for three days before asking. – I wrestled a bear once. Jul 15 '20 at 12:50
  • 1
    @DarrenCook - I get that. I stated that in my question. ***my question is specifically about the constructor.*** – I wrestled a bear once. Jul 15 '20 at 12:51
  • https://stackoverflow.com/a/48052367/476 – deceze Jul 15 '20 at 12:54
  • @deceze - That would be a relevant answer here, but it is mostly opinion based. A relevant answer on another thread is no the same as a duplicate question. – I wrestled a bear once. Jul 15 '20 at 12:59
  • You would need to ask [Brendan](https://en.wikipedia.org/wiki/Brendan_Eich) himself for the reason why they exist. And even then, you'd only get *his opinion*. The fact is they do exist, you know how they behave, there's ample discussion why you shouldn't use them. What would be a possible reason to *hide* those types? As it is, something like `Array.prototype.slice.call(foo)` is a common idiom, only possible because the type and its prototype is exposed. It would seem even weirder if you needed to create an instance using a literal first to access those prototype functions. – deceze Jul 15 '20 at 13:04
  • Thank you @deceze. That pretty much answers my questions. It doesn't explain why you erroneously marked my question as a dupe though. I would have liked to hear from other people on the subject. – I wrestled a bear once. Jul 15 '20 at 13:06

0 Answers0