avatarDr. Derek Austin 🥳

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5017

Abstract

returns, there is another thing about primitive object wrappers that is confusing: <a href="https://www.valentinog.com/blog/coercion/">type coercion</a>.</p><p id="5dea">The same function can be used without the keyword <code>new</code> in order to coerce a value to that primitive type. Here is what that looks like:</p> <figure id="6902"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe src="https://gist.github.com/djD-REK/e3002a2c7e005a4d243bafff4fd93e66.pibb" allowfullscreen="" frameborder="0" height="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><p id="9fcd">So, using the wrapper function without the <code>new</code> keyword is a useful way of coercing a value into a primitive type.</p><p id="58f3">On the other hand, using the <code>new</code> keyword creates an object-wrapped primitive, which is considered <b>bad mojo</b>.</p><figure id="5123"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*_gMwKk9muFvy1oiI"><figcaption>Photo by <a href="https://unsplash.com/@kylejglenn?utm_source=medium&amp;utm_medium=referral">Kyle Glenn</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h1 id="4c7e">Why do wrappers exist if they are bad?</h1><p id="0b51">Basically, these primitive object wrappers only exist in JavaScript to allow primitive types to act like objects.</p><p id="2404">JavaScript automatically calls the primitive object wrapper behind the scenes when the code references a prototype function or property.</p><p id="4676">For example, this occurs when checking the length of a string using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length">String.prototype.length</a> property or replacing all substrings within a string using the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace">String.prototype.replace()</a> function:</p> <figure id="80fb"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe src="https://gist.github.com/djD-REK/a2e332c2840cd84876fb8047a7049e6b.pibb" allowfullscreen="" frameborder="0" height="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><p id="8261">In both of these cases, JavaScript calls the object wrapper under the hood in order to be able to reference the object’s properties and methods.</p><figure id="5813"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*LxPWsPjambLW_VaY"><figcaption>Photo by <a href="https://unsplash.com/@kellysikkema?utm_source=medium&amp;utm_medium=referral">Kelly Sikkema</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h1 id="b49c">Proofing against an object wrapper</h1><p id="f404">What if your code happens to be riddled with tons of primitive object wrappers? In that case, how do you check for a string anyway?</p><p id="8d80">The tip is to know about two additional JavaScript features — the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"><code>instanc</code>eof</a> keyword and the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString"><code>Object.prototype.toString.cal</code>l()</a> function.</p><p id="b5e4">Here is how to use those for an improved <code>isString()</code> function:</p> <figure id="a410"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe src="https://gist.github.com/djD-REK/36c35f142b90622a6a752a5c8b9e8e0e.pibb" allowfullscreen="" frameborder="0" height="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><figure id="82ed"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*6Tc2nHgcSh_LUkJu"><figcaption>Photo by <a href="https://unsplash.com/@amseaman?utm_source=medium&amp;utm_medium=referral">Andrew Seaman</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h1 id="8e35">Conclusion</h1><p id="e235">Generally speaking, <code>typeof</code> will get you pretty far in your day-to-day code when checking for a string.</p><p id="d88f">Usually, objected wrapped Strings only exist as a JavaScript quiz question, but they are easy to handle if you need to check for them.</p><p id="a408">For object wrapped primitives, the <code>instanceof</code> keyword and the <code>Object.prototype.toString.call()</code> function will reveal a value’s type.</p><figure id="4ce9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*pDOF5ICMEPMH-y2Y"><figcaption>Photo by

Options

<a href="https://unsplash.com/@suburbanmillennial?utm_source=medium&amp;utm_medium=referral">Corey Grover</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h1 id="2e8b">Additional resources</h1><ul><li><a href="undefined">Nikhil Swain</a> discussed working with JavaScript strings in <a href="https://readmedium.com/working-with-strings-in-javascript-34060a1c17a9">The Startup</a>:</li></ul><div id="a83d" class="link-block"> <a href="https://readmedium.com/working-with-strings-in-javascript-34060a1c17a9"> <div> <div> <h2>Working with strings in JavaScript</h2> <div><h3>HTML is a text-based, so if you’re reading or writing data on a web page, it’s inevitable that you’re going to have to…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*xK31ldiTMOGyM9n3rLTu7w.png)"></div> </div> </div> </a> </div><ul><li><a href="undefined">Samantha Ming</a> talked about converting values to strings in <a href="https://readmedium.com/5-ways-to-convert-a-value-to-string-in-javascript-6b334b2fc778">Daily JS</a>:</li></ul><div id="8f44" class="link-block"> <a href="https://readmedium.com/5-ways-to-convert-a-value-to-string-in-javascript-6b334b2fc778"> <div> <div> <h2>5 Ways to Convert a Value to String in JavaScript</h2> <div><h3>Let’s check out the different ways of converting a value to a string in JavaScript. The preferred way from Airbnb’s…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*SjOBBal2HQrbXZaapjf7kA.png)"></div> </div> </div> </a> </div><ul><li><a href="undefined">Trey Huffine</a> wrote about many string methods in <a href="https://levelup.gitconnected.com/essential-javascript-string-methods-f1841dad1961">gitconnected</a>:</li></ul><div id="ec13" class="link-block"> <a href="https://levelup.gitconnected.com/essential-javascript-string-methods-f1841dad1961"> <div> <div> <h2>Essential JavaScript String Methods</h2> <div><h3>The 13 most important JavaScript functions for working with strings. Index, slice, split, and trim your way through JS…</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*tifNDBJM5QhgjZSTQYJR-Q.png)"></div> </div> </div> </a> </div><ul><li>The <a href="undefined">CodeDraken</a> covered types and data structures at <a href="https://codeburst.io/javascript-essentials-types-data-structures-3ac039f9877b">codeburst</a>:</li></ul><div id="6817" class="link-block"> <a href="https://codeburst.io/javascript-essentials-types-data-structures-3ac039f9877b"> <div> <div> <h2>JavaScript Essentials: Types & Data Structures</h2> <div><h3>Essentials is a series that covers the most used and important methods for X topic. It’s a series for developers who…</h3></div> <div><p>codeburst.io</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*YiweHQXVp1yE2HGXiKK6Ew.png)"></div> </div> </div> </a> </div><ul><li><a href="undefined">Sonya Moisset</a> demonstrates how to reverse a string at <a href="https://www.freecodecamp.org/news/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb/">freeCodeCamp</a>:</li></ul><div id="7aeb" class="link-block"> <a href="https://www.freecodecamp.org/news/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb/"> <div> <div> <h2>Three Ways to Reverse a String in JavaScript</h2> <div><h3>This article is based on Free Code Camp Basic Algorithm Scripting "Reverse a String…</h3></div> <div><p>www.freecodecamp.org</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*eqrX7SNlaKtdqANS)"></div> </div> </div> </a> </div><p id="5086"><a href="https://www.linkedin.com/in/derek-austin/">Dr. Derek Austin</a> is the author of <a href="https://www.amazon.com/dp/B0BRJDLJ43"><i>Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months</i></a>, now available on Amazon.</p></article></body>

How to check for a string in JavaScript

When checking for strings, typeof works great unless someone uses a primitive object wrapper, which is considered bad code.

Photo by David Clode on Unsplash

Check for a primitive string

Some things are a little tricky to check for in JavaScript, like checking for NaN, but thankfully strings are a lot easier to deal with.

When checking if a value is a string in JavaScript, typeof works great:

Well, typeof does not work as expected in the case of strings created with new String(), which is what is called the primitive object wrapper.

Creating “object-wrapped” strings like this is seldom used and generally recommended against, as I explore in the next section.

Photo by Jeremy Bishop on Unsplash

Why are primitive object wrappers bad?

Because just about everyone online says JavaScript object wrappers are bad, confusing, and should go away. For example:

  1. The Google JavaScript Style Guide says to never, ever use primitive object wrappers, you naughty developers you.
  2. Douglas Crockford publicly recommended that primitive object wrappers be deprecated — that their use be officially discouraged.

In fact, I have never seen anyone recommending their use. Primitive object wrappers appear to be pretty universally reviled.

In the next section, I review what one needs to know about them.

Photo by John Salzarulo on Unsplash

How not to use object wrappers

Really, the only time time wrappers come up is in “Gotcha!” style interview questions, usually regarding type-checking using typeof.

In fact, of the following three ways of invoking a string, I think almost all JavaScript programmers would only even be aware of the first one.

The other two syntaxes should be generally be avoided as confusing:

Photo by Kara Eads on Unsplash

Object wrappers are confusing

“[E]xplicitly creating wrappers using the new operator is considered a bad practice. On the other hand, calling the function without the new operator is perfectly valid as it just tries to convert the input into the corresponding primitive type and returns a primitive value.”

— Vojtěch Růžička on his blog

In addition to changing what typeof returns, there is another thing about primitive object wrappers that is confusing: type coercion.

The same function can be used without the keyword new in order to coerce a value to that primitive type. Here is what that looks like:

So, using the wrapper function without the new keyword is a useful way of coercing a value into a primitive type.

On the other hand, using the new keyword creates an object-wrapped primitive, which is considered bad mojo.

Photo by Kyle Glenn on Unsplash

Why do wrappers exist if they are bad?

Basically, these primitive object wrappers only exist in JavaScript to allow primitive types to act like objects.

JavaScript automatically calls the primitive object wrapper behind the scenes when the code references a prototype function or property.

For example, this occurs when checking the length of a string using the String.prototype.length property or replacing all substrings within a string using the String.prototype.replace() function:

In both of these cases, JavaScript calls the object wrapper under the hood in order to be able to reference the object’s properties and methods.

Photo by Kelly Sikkema on Unsplash

Proofing against an object wrapper

What if your code happens to be riddled with tons of primitive object wrappers? In that case, how do you check for a string anyway?

The tip is to know about two additional JavaScript features — the instanceof keyword and the Object.prototype.toString.call() function.

Here is how to use those for an improved isString() function:

Photo by Andrew Seaman on Unsplash

Conclusion

Generally speaking, typeof will get you pretty far in your day-to-day code when checking for a string.

Usually, objected wrapped Strings only exist as a JavaScript quiz question, but they are easy to handle if you need to check for them.

For object wrapped primitives, the instanceof keyword and the Object.prototype.toString.call() function will reveal a value’s type.

Photo by Corey Grover on Unsplash

Additional resources

Dr. Derek Austin is the author of Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months, now available on Amazon.

JavaScript
Programming
Software Development
Web Development
Coding
Recommended from ReadMedium