Search code examples
javascriptobjectprimitivecasting

What makes the availability of both primitive and object-wrapped values in JavaScript useful?


I wrote a blog post a while ago detailing how the availability of both primitive and object-wrapped value types in JavaScript (for things such as Number, String and Boolean) causes trouble, including but not limited to type-casting to a boolean (e.g. object-wrapped NaN, "" and false actually type-cast to true).

My question is, with all this confusion and problems, is there any benefit to JavaScript having both types of values for the built-in classes?

Edit: Thanks for the quick answers. I think that the creators of JavaScript intended wrapped natives as a way to make scalar values have child methods, but it clearly backfired, causing more problems.


Solution

  • JavaScript, like many languages, has good parts and bad parts.

    This is one of the really really bad parts.

    IMHO, there really isn't much benefit, only harm, from typed wrappers.

    Our friend Douglas Crockford has been all over this issue, in fact he has been against it from day one. That's all you need to know.