Internal Link 2

The following text is an excerpt from Kyle Simpson’s “You don’t know JS” and is intended here for SEO experiments only.

These object wrappers serve a very important purpose. Primitive values don’t have properties or methods, so to access .length or .toString() you need an object wrapper around the value. Thankfully, JS will automatically box (aka wrap) the primitive value to fulfill such accesses.

So, if you’re going to be accessing these properties/methods on your string values regularly, like a i < a.lengthcondition in a for loop for instance, it might seem to make sense to just have the object form of the value from the start, so the JS engine doesn’t need to implicitly create it for you.

But it turns out that’s a bad idea. Browsers long ago performance-optimized the common cases like .length, which means your program will actually go slower if you try to “preoptimize” by directly using the object form (which isn’t on the optimized path).

In general, there’s basically no reason to use the object form directly. It’s better to just let the boxing happen implicitly where necessary. In other words, never do things like new String("abc")new Number(42), etc — always prefer using the literal primitive values "abc" and 42.