<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ricardo Colon Medina on Medium]]></title>
        <description><![CDATA[Stories by Ricardo Colon Medina on Medium]]></description>
        <link>https://medium.com/@rjcolmed?source=rss-ff5986eb9a39------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Q8q1POnRCYTGUbGf45R3kw.jpeg</url>
            <title>Stories by Ricardo Colon Medina on Medium</title>
            <link>https://medium.com/@rjcolmed?source=rss-ff5986eb9a39------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 01:44:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rjcolmed/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Tagged Template Literals in JavaScript]]></title>
            <link>https://medium.com/front-end-weekly/tagged-template-literals-in-javascript-776eaed537a0?source=rss-ff5986eb9a39------2</link>
            <guid isPermaLink="false">https://medium.com/p/776eaed537a0</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ecmascript]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Ricardo Colon Medina]]></dc:creator>
            <pubDate>Fri, 12 Jan 2018 20:57:19 GMT</pubDate>
            <atom:updated>2018-01-12T20:57:19.275Z</atom:updated>
            <content:encoded><![CDATA[<p>Template literals are rad. Most JavaScript developers already know what they area and how to use them, but if you’re just starting out, you might not know about <strong><em>tagged</em></strong> template literals.</p><p>For the sake of making me type more, let’s start off with what template literals are.</p><h4>Template Literals: What Do?</h4><p>In JavaScript, we used to have to do this to interpolate string literals and expressions:</p><pre>var actor = &quot;Michael Keaton<br>var icon = &quot;Mr. Mom&quot;</pre><pre>function returnInaneFactoid(actor, icon) {<br>  return actor + &quot; is &quot; + icon<br>}</pre><pre>returnInaneFactoid(actor, icon) //=&gt; &quot;Michael Keaton is Mr. Mom&quot;</pre><p>Not bad. Not bad. But that can get unwieldy. And, honestly, it looks ugly.</p><p>Template literals give us a cleaner, more powerful (more on that in a bit) way to combine string literals and expressions.</p><p>Peep it:</p><pre>var actor = &quot;Michael Keaton&quot;<br>var icon = &quot;Batman&quot;</pre><pre>function returnInaneFactoid(actor, icon) {<br>  return `${actor} is ${icon}`<br>}</pre><pre>returnInaneFactoid(actor, icon) =&gt; &quot;Michael Keaton is Batman&quot;<br></pre><p>That’s alright, but if we break down template literals, we get to see a little more of the Power of Grayskull.</p><p>A template literal consists of four parts, two visible and one invisible.</p><p><strong>I see you</strong>:</p><ul><li>Back-ticks delimiting the entire literal: ``</li><li>Placeholders surrounding expressions: ${expression_goes_here}</li><li>Chunks of string (like &quot;is&quot; in the example above)</li></ul><p><strong>I don’t see you:</strong></p><ul><li>An invisible, default function—right in front of the first back-tick—to which the expressions inside the placeholders and the chunks of string are passed.</li></ul><p>The invisible function’s default behavior is to concatenate the evaluated expressions and chunks of strings into <em>a string</em>. Thus, the output from our example: &quot;Michael Keaton is Batman&quot; .</p><p>You probably didn’t know this function existed, I certainly didn’t. But it does exist, just like ghosts, and you can replace it with your own, unlike ghosts. When you do replace it, your function is called a <strong>tag</strong> and you, my friend, you’ve got yourself a <strong>tagged template literal</strong>.</p><h4>Tagged Template Literals</h4><p>You might be thinking, “Tagged? What is this, a New York subway car in the ‘70s?”.</p><p>That’s a weird thought. It’s 2018, so no. As I mentioned before, that function you place in front of the template literal is called a <strong>tag</strong>, and you can use it to process the chunks of string and expressions before they’re concatenated into a single string.</p><p>Tag functions takes two sets of arguments.</p><ul><li><em>Argument set 1</em>: An array containing all of the following template literal’s string chunks</li><li><em>Argument set 2</em>: One parameter for each expression in the following template literal</li></ul><p>You can name tag functions whatever you’d like, too, which means you can create a nice, reusable utility to, say, localize currency amounts from expressions embedded into a template string.</p><p>I don’t want to type that out right now, so let’s look at a simple, contrived example to get a better grasp of the above.</p><pre>function opine(strings, ...expressionValues) {</pre><pre>  var finalString = &#39;&#39;</pre><pre>  for ( let i = 0; i &lt; strings.length; i++ ) {</pre><pre>    if (i &gt; 0) {</pre><pre>      finalString += expressionValues[i - 1].toUpperCase()</pre><pre>    }</pre><pre>    finalString += strings[i]</pre><pre>  }</pre><pre>  return finalString</pre><pre>}</pre><pre>const celebrityUndeservingOfPraise = &quot;Lena Dunham&quot;<br>const anotherCelebrityUnderservingOfPraise = &quot;Donald Trump&quot;</pre><pre>console.log(opine`Nobody likes you,  ${celebrityUndeservingOfPraise} and ${anotherCelebrityUnderservingOfPraise}`)</pre><pre>//This prints out: &quot;Nobody likes you, LENA DUNHAM and DONALD TRUMP&quot;</pre><p>A few notes:</p><p>Notice I used the rest operator to gather the comma-separated arguments into an array, which I indexed and up-cased.</p><p>Also notice the conditional check (if i &gt; 0) . I added this because there is always one more string than there is an expression in a string literal. To clarify: in a string literal, even it looks like it begins or ends with an expression (e.g. `${expression}) there is always an empty string at the beginning and end of the literal.</p><p>In my little example here, the arguments passed into the opine function would be:</p><pre>opine([&quot;Nobody likes you, &quot;, &quot;and&quot;, &quot;&quot;], &quot;Lena Dunham&quot;, &quot;Donald Trump&quot;) </pre><p>The strings array has a length of 3 and the expressionValues array (which was gathered by the rest operator) has a length of 2.</p><p>The conditional check (if i &gt; 0), then, allows me to ignore one of the string indices (because, remember, I’m looping over the strings array, and arrays begin at 0), before I execute the first block of code to up-case the value from the first expression in the expression array.</p><p><strong>My Fingers Hurt</strong></p><p>You can see now how cool and useful tagged template literals can be, right? Play around with them and see what you can come up with.</p><p>Before I go put Icy Hot on my hands from typing so much, a big shout out to Kyle Simpson’s course “ES6: The Right Parts” on Lynda.com for elucidating tagged template literals for me. Check it out if you’re interested in learning some more cool stuff about ES6 JavaScript.</p><p>Alright. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=776eaed537a0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/tagged-template-literals-in-javascript-776eaed537a0">Tagged Template Literals in JavaScript</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating Arrays from Array-like Objects]]></title>
            <link>https://medium.com/front-end-weekly/creating-arrays-from-array-like-objects-5d24815cdbd3?source=rss-ff5986eb9a39------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d24815cdbd3</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[es2015]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ricardo Colon Medina]]></dc:creator>
            <pubDate>Thu, 11 Jan 2018 17:23:10 GMT</pubDate>
            <atom:updated>2018-01-11T17:23:10.159Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello there.</p><p>During my deeper travels into JavaScript, I’ve encountered a few useful ways to convert Array-like objects into arrays, so I thought I’d share. Maybe this’ll save someone time or spark a ‘eureka’-like moment. <strong>NB</strong>: To experience the latter, you <a href="https://en.wikipedia.org/wiki/Eureka_(word)"><strong>have to be in a bathtub</strong></a><strong>.</strong></p><h4>Objects Pretending to Be Arrays</h4><p>First, though, what is an Array-like object? It’s something that looks like an array, but isn’t.</p><p>An Array-like object:<br><br>Contains properties with numeric indices<br> *Has a .length property<br> *Lacks the methods you’d expect an Array object to have, e.g. .push, .pop, .forEach.</p><p>Have you ever tried to use .forEach on a bunch of elements you gathered with document.getElementsByName? Yeah? If so, you’ve run into an array-like object, namely an HTMLCollection. Similar thereto is a NodeList, which you’d encounter if you tried to gather your divs with something like document.querySelectorAll(‘div’).</p><p>Another commonly-encountered Array-like object is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments">arguments</a>. If you’ve never run in to arguments or had cause to, it’s a local variable — more specifically, an object — available within all non-arrow functions.</p><p>Here’s a simple piece of code you can use to interact with arguments:</p><pre>function getArguments() {<br>return arguments;<br>}</pre><pre>getArguments(‘flippity’, ‘floppity’);</pre><p>Copy and paste that into Chrome’s dev console and you’ll see that it returns an arguments object.</p><h3>Helping Non-Arrays Be All They Can Be</h3><p>Now that we have a good idea of what an Array-like object is, and where we might encounter it in the wild, how do we convert them into Array objects, so we can take advantage of useful methods, e.g. .forEach?</p><p>Let’s say you want to iterate over, and do something with, a bunch of divs you gathered from a page. Normally, you wouldn’t be able to, or would have to make use of a for loop, like so:</p><pre>const divs = document.getElementsByTagName(‘div’);</pre><pre>for ( let i = 0; i &lt; divs.length; i++ ) { //do something with each div }</pre><p>If you were so inclined, you could convert the HTMLCollection of divs you’ve got on your hands into an array using one of the following:</p><h4>Array.prototype.slice() and .call()</h4><p>You can call on the Array object to help you out by chaining its .<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice">slice</a> method and .<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call">call</a>. Using our HTMLCollection as an example:</p><pre>const newArray = Array.prototype.slice.call(document.getElementsByTagName(‘div’));</pre><pre>newArray.forEach(div =&gt; //do something with div);</pre><h4>[].slice() and .call()</h4><p>[] is a proxy for Array.prototype, which means less typing, which is good for our fingers.</p><p>Using our HTMLCollection as an example:</p><pre>const newArray = [].slice.call(document.getElementsByTagName(‘div’));</pre><pre>newArray.forEach(div =&amp;gt; //do something with div);</pre><h4>Array.from()</h4><p>Continuing our petition to the Array object, we can also use<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from">Array.from</a>. <strong>NB</strong>: This method won’t work in Internet Explorer without a bit of extra help (see its MDN page, linked above, for more info).</p><p>Using our HTMLCollection as an example:</p><pre>const newArray = Array.from(document.getElementsByTagName(‘div’));</pre><pre>newArray.forEach(div =&amp;gt; //do something with div);</pre><h4>[…]</h4><p>Finally, the spread operator. It’s a fantastic way to convert Array-like objects into honest-to-God arrays. It’s also cleaner and pithier than the previously-listed methods. One downside, however, is that it’s a bit less semantic than Array.from, so if you’re into writing über-semantic code, you’ve got that.</p><p>Using our HTMLCollection as an example:</p><pre>const newArray = […document.getElementsByTagName(‘div’)]</pre><pre>newArray.forEach(div =&gt; //do something with div);</pre><p>In fact, we can do the following and join the chain gang:</p><pre>[…document.getElementsByTagName(‘div’)].forEach(div =&gt; //do something with div);</pre><p>Alright. That’s all for now. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d24815cdbd3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-weekly/creating-arrays-from-array-like-objects-5d24815cdbd3">Creating Arrays from Array-like Objects</a> was originally published in <a href="https://medium.com/front-end-weekly">Frontend Weekly</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>