<?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 Osama Malik on Medium]]></title>
        <description><![CDATA[Stories by Osama Malik on Medium]]></description>
        <link>https://medium.com/@malikk908?source=rss-967501fa3b8e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*pJ3s4Sv69QkxyZSx</url>
            <title>Stories by Osama Malik on Medium</title>
            <link>https://medium.com/@malikk908?source=rss-967501fa3b8e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 12:07:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@malikk908/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[JavaScript — How to Count Number of Words in a String — JS Word Count — Beginner Guide]]></title>
            <link>https://medium.com/@malikk908/javascript-how-to-count-number-of-words-in-a-string-js-word-count-beginner-guide-fe759ca95017?source=rss-967501fa3b8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe759ca95017</guid>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[word-count-tool]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Osama Malik]]></dc:creator>
            <pubDate>Tue, 04 Jul 2023 15:03:34 GMT</pubDate>
            <atom:updated>2023-07-04T15:03:34.834Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript — How to Count Number of Words in a String — JS Word Count — Beginner Guide</h3><p>This short tutorial will show you how to count words in a JavaScript string in the easiest possible way with the explanation of each and every step!</p><p>First of all, we declare a string <em>str, </em>split it a store it in an array called<em> indElem </em>as shown below:</p><pre>let str = &#39;How are you?&#39;<br><br>let indElem = str.split(&#39; &#39;)<br><br>console.log(indElem)</pre><p><strong>Output: </strong>We can see that the split function has returned an array of all the individual strings separated by a single space stored in the original string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*xHf7Dr2kIIJK8wTh_GB3Rg.png" /></figure><p>Now Word Count can be calculated easily using the <em>.length </em>as follows:</p><pre>let str = &#39;How are you?&#39;<br><br>let indElem = str.split(&#39; &#39;)<br><br>console.log(indElem)<br><br>//<br><br>let wordCount = indElem.length<br><br>console.log(wordCount)</pre><p><strong>Output: </strong>As we can see, the length of the array indElem indicates the number of words stored in it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*39RRTWDb1qBTbKHth3ldRA.png" /></figure><p><strong>Wait Wait…….!</strong></p><p>What if your original string contains some extra spaces? You definitely won’t like to count those as words too; look at the example below:</p><pre>let str = &#39;How   are   you?&#39;<br><br>let indElem = str.split(&#39; &#39;)<br><br>console.log(indElem)<br><br>let wordCount = indElem.length<br><br>console.log(wordCount)</pre><p><strong>Output</strong>: Notice the <em>indElem </em>now, it contains the spaces now which you want to remove.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*ql4fgSzaMhSHg_aOSsc5UQ.png" /></figure><p>Now let’s create a new variable <em>newIndElem </em>and we apply a <em>filter </em>to the <em>indElem </em>(which contains spaces) so we can remove the spaces.</p><p>We know that we have to pass a function to the <em>filter </em>function which then returns something based on the condition we provide. Lets look at the code below:</p><pre>let str = &#39;How   are   you?&#39;<br><br>let indElem = str.split(&#39; &#39;)<br><br>console.log(indElem)<br><br>let wordCount = indElem.length<br><br>console.log(wordCount)<br><br>//<br><br>let newIndElem = indElem.filter((x)=&gt;{<br>  return x.length != 0})<br><br>console.log(newIndElem)<br><br>let newWordCount = newIndElem.length<br><br>console.log(newWordCount)</pre><p>In this case, we are providing an arrow function which basically says that:</p><p>“Go and check all the values in the <em>indElem, </em>if you find any value whose <strong>length </strong>is NOT Equal to 0, return that value to our new variable <em>newIndElem”</em></p><p>Since blank spaces will have a length of 0, those will be excluded from our newly created <em>newIndElem</em></p><p><strong>Output</strong>: As expected, our <em>newIndElem </em>now contains all the strings except the spaces and thus when we calculate its length, it will work perfectly fine even if the original string contains as many spaces as it can.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*m3UPhEUYE0ShcZNZb3E4_Q.png" /></figure><p>Now that’s a lot of lines of code for a small problem like this, so lets start optimizing it so it takes least amount of space.</p><pre>let str = &#39;How   are   you?&#39;<br><br>let wordCount = str.split(&#39; &#39;).filter( (x) =&gt; {return x.length != 0} ).length<br><br>console.log(wordCount)</pre><p>So, what we did was to combine all the steps into a single line of code!</p><p>If this helped you, make sure to follow me for more helpful tips related to JavaScript &amp; React.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe759ca95017" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>