<?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"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://blog.tomok.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1663132163945/xyQfJpi6k.png</url><title>Untitled Publication</title><link>https://blog.tomok.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 18:09:40 GMT</lastBuildDate><atom:link href="https://blog.tomok.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Asynchronous JavaScript]]></title><description><![CDATA[Asynchronous JavaScript is one of the most important concepts in JavaScript as it is deeply connected with how to run your code smoothly and efficiently. For example, there might be some potential blockage in your code when you fetch data from the se...]]></description><link>https://blog.tomok.dev/asynchronous-javascript</link><guid isPermaLink="true">https://blog.tomok.dev/asynchronous-javascript</guid><category><![CDATA[asynchronous]]></category><category><![CDATA[promises]]></category><category><![CDATA[trycatch]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Tomo]]></dc:creator><pubDate>Mon, 19 Sep 2022 01:20:29 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1663550092837/UphcfEvIk.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Asynchronous JavaScript is one of the most important concepts in JavaScript as it is deeply connected with how to run your code smoothly and efficiently. For example, there might be some potential blockage in your code when you fetch data from the server and you need to know how to handle it. </p>
<blockquote>
<p>Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result.</p>
</blockquote>
<p>One of the long-running tasks that you would often encounter is making HTTP requests using <code>fetch()</code>.</p>
<h2 id="heading-synchronous-programming">Synchronous programming</h2>
<p>To understand what asynchronous programming means better, let's look at an example of synchronous programming.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> pet = <span class="hljs-string">'pug'</span>
<span class="hljs-keyword">const</span> statement = <span class="hljs-string">`I want to have a <span class="hljs-subst">${pet}</span>!`</span>;
<span class="hljs-built_in">console</span>.log(statement);

<span class="hljs-comment">// Output</span>
I want to have a pug!
</code></pre>
<p>The above code is a synchronous program because the code is executed line by line in order from top to bottom. Another example would be: </p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">favAnimal</span>(<span class="hljs-params">animal</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-string">`My favourite animal is a <span class="hljs-subst">${animal}</span>!`</span>;
}

<span class="hljs-keyword">const</span> animal = <span class="hljs-string">'dog'</span>;
<span class="hljs-keyword">const</span> sayFavAnimal = favAnimal(animal);
<span class="hljs-built_in">console</span>.log(sayFavAnimal);

<span class="hljs-comment">// Output</span>
My favourite animal is a dog!
</code></pre>
<p>The above is also a synchronous program although it didn't run like the previous example. It's because <code>favAnimal()</code> function needed to wait for the function to finish its work and return a value before the caller can continue. </p>
<h2 id="heading-asynchronous-programming">Asynchronous programming</h2>
<p>So far by looking at some of the example codes, you may notice that the keywords to explain synchronous programming are "line by line" or "wait". So asynchronous programming is the opposite of that - no need to run a code line by line and no need to wait. </p>
<p>What if the task of the synchronous function takes a long time? There would be a serious problem because your program won't respond or allow your users to do anything until the task is completed. That's when you should consider implementing the asynchronous program. </p>
<h3 id="heading-event-handlers">Event handlers</h3>
<p>One of the examples of asynchronous programming is event handlers. Your function (the event handler) will be called whenever the event happens, which is not a straight way and you need to wait until it happens. </p>
<h3 id="heading-promise">Promise</h3>
<p>Next, let's look into another example of asynchronous programming.</p>
<blockquote>
<p>A promise is an object returned by an asynchronous function, which represents the current state of the operation. At the time the promise is returned to the caller, the operation often isn't finished, but the promise object provides methods to handle the eventual success or failure of the operation.</p>
</blockquote>
<p>To understand how <code>promise</code> works, let's make an HTTP request to the server.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> fetchPromise = fetch(<span class="hljs-string">'https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'</span>);

<span class="hljs-built_in">console</span>.log(fetchPromise);  <span class="hljs-comment">// Promise {&lt;pending&gt;}</span>

fetchPromise
  .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> {
    <span class="hljs-keyword">if</span> (!response.ok) {
      <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">`HTTP error: <span class="hljs-subst">${response.status}</span>`</span>);
    }
    <span class="hljs-keyword">return</span> response.json();
  })
  .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(data[<span class="hljs-number">0</span>].name))
  .catch(<span class="hljs-function"><span class="hljs-params">error</span> =&gt;</span> <span class="hljs-built_in">console</span>.error(<span class="hljs-string">`Could not get products: <span class="hljs-subst">${error}</span>`</span>));

<span class="hljs-comment">// Output</span>
baked beans
</code></pre>
<p>Let's look into the above in detail. </p>
<ol>
<li>We called <code>fetch()</code> to make an HTTP request. </li>
<li>As <code>console.log()</code> result shows, <code>Promise</code> object is returned. It has a state whose value is <code>pending</code>, which means the fetch operation is still going on. </li>
<li>We passed a handler function into the <code>Promise</code>'s <code>then()</code> method. When (and if) the server accepted and was able to handle the request, we return <code>response.json()</code>. We can return the promise returned by json() on the first <code>then()</code>, and call the second then() on that return value.</li>
<li>We again call the second <code>then()</code> method. Inside, we can handle the data that has been returned from the server. </li>
<li>If you add <code>catch()</code> to the end of a promise chain, then it will be called when any of the asynchronous function calls fail. So you can implement an operation as several consecutive asynchronous function calls, and have a single place to handle all errors.</li>
</ol>
<p>What if you would like to fetch multiple data? In that case, we can use <code>Promise.all()</code> method. It returns promises when they are all fulfilled. This means if any of the promises in an array is rejected, it results reject. </p>
<h3 id="heading-async-and-await">async and await</h3>
<p>The <code>async</code> keyword gives you a simpler way to work with asynchronous promise-based code. </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fetchData</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(<span class="hljs-string">'https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json'</span>)

        <span class="hljs-keyword">if</span> (!response.ok) {
          <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">`HTTP error: <span class="hljs-subst">${response.status}</span>`</span>);
        }
        <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> response.json()
        <span class="hljs-built_in">console</span>.log(data[<span class="hljs-number">0</span>].name);
    } <span class="hljs-keyword">catch</span> (error) {
    <span class="hljs-built_in">console</span>.error(<span class="hljs-string">`Could not get products: <span class="hljs-subst">${error}</span>`</span>);
  }
}

fetchData();

<span class="hljs-comment">// Output </span>
baked beans
</code></pre>
<p>Inside an async function, you can use the <code>await</code> keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown. Here <code>try...catch</code> block is used for error handling instead of <code>catch()</code>. </p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>We have taken a look at what asynchronous programming means in JavaScript along with examples. This is a very important concept to understand as a JavaScript developer as it significantly affects your code performance. </p>
]]></content:encoded></item><item><title><![CDATA[Array Methods - Most Useful Ones with Examples]]></title><description><![CDATA[As a JavaScript developer, it's important that you are familiar with array methods and how to handle the data in an array format. In this article, I will summarise some of the most commonly used array methods along with examples to understand them be...]]></description><link>https://blog.tomok.dev/array-methods-most-useful-ones-with-examples</link><guid isPermaLink="true">https://blog.tomok.dev/array-methods-most-useful-ones-with-examples</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[js]]></category><category><![CDATA[array methods]]></category><category><![CDATA[array, javascript, array methods, map, filter, forEach, ]]></category><category><![CDATA[sorting]]></category><dc:creator><![CDATA[Tomo]]></dc:creator><pubDate>Thu, 15 Sep 2022 12:45:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1663288057845/hwkcC8ym-.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a JavaScript developer, it's important that you are familiar with array methods and how to handle the data in an array format. In this article, I will summarise some of the most commonly used array methods along with examples to understand them better. </p>
<h2 id="heading-arrayforeach-method">Array.forEach() Method</h2>
<p>The <code>forEach()</code> method loops through an array and execute a provided function once for each array element.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Inline callback function</span>
array.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">currentValue, index, arr</span>) </span>{ 
      <span class="hljs-comment">// Do what you like to do</span>
}, thisValue);

<span class="hljs-comment">// Arrow function</span>
array.forEach(<span class="hljs-function">(<span class="hljs-params">currentValue, index, arr</span>) =&gt;</span> { 
      <span class="hljs-comment">// Do what you like to do</span>
}, thisValue);
</code></pre>
<p>The syntax above covers two main ways of writing the <code>forEach()</code> method. </p>
<p>The callback function is specified in the <code>forEach()</code> method and executes only once for each element. It can accept the specified arguments to be used within the callback function. </p>
<p>Some of the parameters passed would be optional depending on what you like to do.</p>
<ul>
<li><p><code>currentValue</code> is the current element from the array that is being processed. This is the only parameter that is required. </p>
</li>
<li><p><code>index</code> is the index of <code>currentValue</code>. This is optional.</p>
</li>
<li><p><code>arr</code> is the array that was passed to the <code>forEach()</code> method.</p>
</li>
<li><p><code>thisValue</code> is the value that will be used in the callback function. This is optional.</p>
</li>
</ul>
<p>Let's look into some examples to see how the <code>forEach()</code> method works with real data.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> dogs = [<span class="hljs-string">"Pug"</span>, <span class="hljs-string">"Shiba inu"</span>, <span class="hljs-string">"Rottweiler"</span>, <span class="hljs-string">"Golden retriever"</span>, <span class="hljs-string">"German shepherd"</span>];
<span class="hljs-keyword">const</span> pluralS = <span class="hljs-string">"s"</span>;

dogs.forEach(<span class="hljs-function">(<span class="hljs-params">currentDog, index</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span>(index &gt; <span class="hljs-number">1</span>) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`I have <span class="hljs-subst">${index}</span> <span class="hljs-subst">${currentDog}</span><span class="hljs-subst">${pluralS}</span>.`</span>);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`I have <span class="hljs-subst">${index}</span> <span class="hljs-subst">${currentDog}</span>.`</span>);
  }
});

<span class="hljs-comment">// Output</span>
I have <span class="hljs-number">0</span> Pug.
I have <span class="hljs-number">1</span> Shiba inu.
I have <span class="hljs-number">2</span> Rottweilers.
I have <span class="hljs-number">3</span> Golden retrievers.
I have <span class="hljs-number">4</span> German shepherds.
</code></pre>
<p>The <code>forEach()</code> method is useful when you want to loop through an array of any type of item. You could achieve the same results with <code>for loop</code>, however the <code>forEach()</code> method provides you with cleaner codes. </p>
<p>The <code>forEach()</code> method is also handy when you want to iterate an array but you don't want to return anything. If you want to return value, consider using the <code>map()</code> method that we are going to look into next.</p>
<h2 id="heading-arraymap-method">Array.map() Method</h2>
<p>The <code>map()</code> method is one of the most used and popular methods on my list in the article. It executes a provided function once for every element in the array and it returns a new array that is created. </p>
<p>The syntax of the <code>map()</code> methods are as follows:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Inline callback function</span>
array.map(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">currentValue, index, arr</span>) </span>{ 
      <span class="hljs-comment">// Do what you like to do</span>
}, thisValue);

<span class="hljs-comment">// Arrow function</span>
array.map(<span class="hljs-function">(<span class="hljs-params">currentValue, index, arr</span>) =&gt;</span> { 
      <span class="hljs-comment">// Do what you like to do</span>
}, thisValue);
</code></pre>
<p>The parameters options are the same as the <code>forEach()</code> method -  <code>currentValue</code> is required and others are optional. Now, let's look at some of the examples.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Example 1</span>
<span class="hljs-keyword">const</span> dogsArray = [<span class="hljs-string">"Pug"</span>, <span class="hljs-string">"Shiba inu"</span>, <span class="hljs-string">"Rottweiler"</span>, <span class="hljs-string">"Golden retriever"</span>, <span class="hljs-string">"German shepherd"</span>];

<span class="hljs-keyword">const</span> newDogsArray = dogsArray.map(<span class="hljs-function"><span class="hljs-params">dog</span> =&gt;</span> dog.concat(<span class="hljs-string">"s"</span>));
<span class="hljs-built_in">console</span>.log(newDogsArray);

<span class="hljs-comment">// Output</span>
[<span class="hljs-string">'Pugs'</span>, <span class="hljs-string">'Shiba inus'</span>, <span class="hljs-string">'Rottweilers'</span>, <span class="hljs-string">'Golden retrievers'</span>, <span class="hljs-string">'German shepherds'</span>]

<span class="hljs-comment">// Example 2</span>
<span class="hljs-keyword">const</span> dogsDetails = [
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"pug"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"light brown"</span>}, 
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"shiba inu"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1.2</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"brown"</span>},
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"rottweiler"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">0.5</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"brown and white"</span>},
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"golden retriever"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>},
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"german shepherd"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1.3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>},
];

<span class="hljs-keyword">const</span> newDogsDetails = dogsDetails.map(<span class="hljs-function"><span class="hljs-params">dog</span> =&gt;</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-string">`My <span class="hljs-subst">${dog.type}</span> is <span class="hljs-subst">${dog.age}</span> years old.`</span>;
});
<span class="hljs-built_in">console</span>.log(newDogsDetails);

<span class="hljs-comment">// Output</span>
[<span class="hljs-string">'My pug is 2 years old.'</span>, <span class="hljs-string">'My shiba inu is 1.2 years old.'</span>, 
  <span class="hljs-string">'My rottweiler is 0.5 years old.'</span>, <span class="hljs-string">'My golden retriever is 3 years old.'</span>, 
  <span class="hljs-string">'My german shepherd is 1.3 years old.'</span>]
</code></pre>
<p>The <code>map()</code> method is useful because you can create a new array without affecting the original array. However, don't forget to return the value except when you write an arrow function with one line of statement code. </p>
<h2 id="heading-arrayreduce-method">Array.reduce() Method</h2>
<p>The <code>reduce()</code> method executes a reducer function (that you provide) on each element of the array, and it returns a single output value. </p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Inline callback function</span>
array.reduce(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">previousValue, currentValue, currentIndex, arr</span>) </span>{
      <span class="hljs-comment">// Do what you like to do</span>
}, initialValue)

<span class="hljs-comment">// Arrow function</span>
array.reduce(<span class="hljs-function">(<span class="hljs-params">previousValue, currentValue, currentIndex, arr</span>) =&gt;</span> {
      <span class="hljs-comment">// Do what you like to do</span>
}, initialValue);
</code></pre>
<p>Let's look into the parameters in detail. </p>
<ul>
<li><p><code>previousValue</code> is the result from the previous call to the callback function. For the first round, it will be <code>array[0]</code> unless <code>initialValue</code> is specified.</p>
</li>
<li><p><code>currentValue</code> is the current element from the array that is being processed. For the first round, it will be <code>array[1]</code> if there is no <code>initialValue</code> specified and it will be <code>array[0]</code> if the the<code>initialValue</code> is specified.</p>
</li>
<li><p><code>currentIndex</code> is the index of <code>currentValue</code>. On the first call, it is <code>0</code> if initialValue was specified, otherwise it is<code>1</code>.</p>
</li>
<li><p><code>arr</code> is the array that was passed to the <code>reduce()</code> method.</p>
</li>
<li><p><code>initialValue</code> is the starting value to the accumulation and <code>previousValue</code> when the callback is called for the first time.</p>
</li>
</ul>
<p>When you read the explanation and each meaning, it can be confusing. The simplest way to understand the <code>reduce()</code> method is to return the sum of all the elements in an array. Let's look at some examples now.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numsArray = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];

<span class="hljs-keyword">const</span> sum = numsArray.reduce(<span class="hljs-function">(<span class="hljs-params">accumulator, number</span>) =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"accumulator: "</span> + accumulator); <span class="hljs-comment">// To see what is happening</span>
  <span class="hljs-keyword">return</span> accumulator + number; 
}, <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(sum);

<span class="hljs-comment">// Output</span>
accumulator: <span class="hljs-number">0</span> <span class="hljs-comment">// This is because we set `initialValue` to be `0`</span>
<span class="hljs-attr">accumulator</span>: <span class="hljs-number">1</span>
<span class="hljs-attr">accumulator</span>: <span class="hljs-number">3</span>
<span class="hljs-attr">accumulator</span>: <span class="hljs-number">6</span>
<span class="hljs-attr">accumulator</span>: <span class="hljs-number">10</span>
<span class="hljs-number">15</span> <span class="hljs-comment">// sum of all the elements in an array</span>
</code></pre>
<p>Now we understand what's happening behind the scenes, let's look into another example.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numsArray = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">551</span>, <span class="hljs-number">39</span>, <span class="hljs-number">38</span>, <span class="hljs-number">20</span>, <span class="hljs-number">5</span>, <span class="hljs-number">10</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">20</span>, <span class="hljs-number">20</span>];

<span class="hljs-keyword">const</span> sortedNumsObj = numsArray.reduce(<span class="hljs-function">(<span class="hljs-params">acc, current</span>) =&gt;</span> { 
    acc[current] ? acc[current]++ : acc[current] = <span class="hljs-number">1</span>    
  <span class="hljs-keyword">return</span> acc;
}, {});
<span class="hljs-built_in">console</span>.log(sortedNumsObj);

<span class="hljs-comment">// Output</span>
{<span class="hljs-number">1</span>: <span class="hljs-number">4</span>, <span class="hljs-number">2</span>: <span class="hljs-number">2</span>, <span class="hljs-number">4</span>: <span class="hljs-number">1</span>, <span class="hljs-number">5</span>: <span class="hljs-number">1</span>, <span class="hljs-number">10</span>: <span class="hljs-number">1</span>, <span class="hljs-number">20</span>: <span class="hljs-number">3</span>, <span class="hljs-number">38</span>: <span class="hljs-number">1</span>, <span class="hljs-number">39</span>: <span class="hljs-number">1</span>, <span class="hljs-number">551</span>: <span class="hljs-number">1</span>}
</code></pre>
<p>The above example demonstrates how to count the same elements in the array. It returns in an object format with the element as keys and the number of elements as values because we set the initial value to be an empty object. It's very useful to sort the data and know how many of each exist but don't forget to return the result, which is the accumulator in this case. </p>
<h2 id="heading-arraysort-method">Array.sort() Method</h2>
<p>The <code>sort()</code> method is useful to sort out the array of data. The default sort order is ascending but you can control the order once you understand how it works. </p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Inline compare function</span>
array.sort(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">a, b</span>) </span>{ 
      <span class="hljs-comment">// Do what you like to do</span>
})

<span class="hljs-comment">// Arrow function</span>
array.sort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> {
      <span class="hljs-comment">// Do what you like to do</span>
} )
</code></pre>
<p>Simply pass 2 parameters that you want to compare: </p>
<ul>
<li><code>a</code> is the first element for comparison.</li>
<li><code>b</code> is the second element for comparison. </li>
</ul>
<p>Let's start looking at some examples with numbers array and then move on to see how alphabetical order can be handled. </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> numsArray = [<span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>];

<span class="hljs-keyword">const</span> ascending = numsArray.sort();
<span class="hljs-built_in">console</span>.log(ascending);

<span class="hljs-keyword">const</span> descending = numsArray.sort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> b - a);
<span class="hljs-built_in">console</span>.log(descending);


<span class="hljs-comment">// Output</span>
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
[<span class="hljs-number">5</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> dogs = [<span class="hljs-string">"Pug"</span>, <span class="hljs-string">"Shiba inu"</span>, <span class="hljs-string">"Rottweiler"</span>, <span class="hljs-string">"Golden retriever"</span>, <span class="hljs-string">"German shepherd"</span>];

<span class="hljs-comment">// Alphabetical order</span>
<span class="hljs-keyword">const</span> sortedDogs = dogs.sort();
<span class="hljs-built_in">console</span>.log(sortedDogs);

<span class="hljs-comment">// Output</span>
[<span class="hljs-string">'German shepherd'</span>, <span class="hljs-string">'Golden retriever'</span>, <span class="hljs-string">'Pug'</span>, <span class="hljs-string">'Rottweiler'</span>, <span class="hljs-string">'Shiba inu'</span>]

<span class="hljs-keyword">const</span> dogsDetails = [
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"pug"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"light brown"</span>}, 
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"shiba inu"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1.2</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"brown"</span>},
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"rottweiler"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">0.5</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"brown and white"</span>},
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"golden retriever"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>},
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"german shepherd"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1.3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>},
];

<span class="hljs-keyword">const</span> sortedDogsDetails = dogsDetails.sort(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> a.type &lt; b.type ? <span class="hljs-number">-1</span> : <span class="hljs-number">1</span>;
});

<span class="hljs-comment">// Output</span>
[{<span class="hljs-attr">type</span>: <span class="hljs-string">'german shepherd'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1.3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">'black'</span>}
  {<span class="hljs-attr">type</span>: <span class="hljs-string">'golden retriever'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">'black'</span>}
  {<span class="hljs-attr">type</span>: <span class="hljs-string">'pug'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">'light brown'</span>}
  {<span class="hljs-attr">type</span>: <span class="hljs-string">'rottweiler'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">0.5</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">'brown and white'</span>}
  {<span class="hljs-attr">type</span>: <span class="hljs-string">'shiba inu'</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1.2</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">'brown'</span>}]
</code></pre>
<p>The comparator always returns either <code>-1</code>, <code>0</code>, or <code>1</code>. If the two values' comparison evaluates something negative, the first element is placed before the second one in an array. If it evaluates positive, the first element comes after the second one. If it returns 0, then the two elements are equal. </p>
<p>As explained, it is handy to sort your data array but be mindful that the <code>sort()</code> method overwrites the original array.</p>
<h2 id="heading-arrayfilter-method">Array.filter() Method</h2>
<p>The <code>filter()</code> method returns a new array that meets the given condition. The syntax is shown as follows:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Inline callback function</span>
array.filter(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">currentValue, index, arr</span>) </span>{ 
      <span class="hljs-comment">// Do what you like to do</span>
}, thisValue);

<span class="hljs-comment">// Arrow function</span>
array.filter(<span class="hljs-function">(<span class="hljs-params">currentValue, index, arr</span>) =&gt;</span> { 
      <span class="hljs-comment">// Do what you like to do</span>
}, thisValue);
</code></pre>
<p>The <code>filter()</code> method always returns an array, which means if you don't pass any parameters it will return an empty array. Now let's check some examples. </p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> dogsDetails = [
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"pug"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"light brown"</span>}, 
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"shiba inu"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1.2</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"brown"</span>},
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"rottweiler"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">0.5</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"brown and white"</span>},
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"golden retriever"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>},
    {<span class="hljs-attr">type</span>: <span class="hljs-string">"german shepherd"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1.3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>},
];

<span class="hljs-keyword">const</span> filteredArray = dogsDetails.filter(<span class="hljs-function"><span class="hljs-params">dog</span> =&gt;</span> dog.age &gt; <span class="hljs-number">1.2</span>);
<span class="hljs-built_in">console</span>.log(filteredArray);

<span class="hljs-comment">// Outcome</span>
[{<span class="hljs-attr">type</span>: <span class="hljs-string">"pug"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"light brown"</span>}, 
  {<span class="hljs-attr">type</span>: <span class="hljs-string">"golden retriever"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>},
  {<span class="hljs-attr">type</span>: <span class="hljs-string">"german shepherd"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">1.3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>}];
</code></pre>
<p>The <code>filter()</code> comes in convenient if you want to filter your data with the condition. To expand the scenario, let's consider the following cases and the better functions that you may want to use instead of the <code>filter()</code>.</p>
<h3 id="heading-every-only-if-you-want-to-know-truefalse-if-the-condition-meets-with-all-of-the-elements-in-an-arrray"><code>every()</code>: Only if you want to know true/false if the condition meets with all of the elements in an arrray</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> filteredArray2 = dogsDetails.every(<span class="hljs-function"><span class="hljs-params">dog</span> =&gt;</span> dog.age &gt; <span class="hljs-number">0.5</span>);
<span class="hljs-built_in">console</span>.log(filteredArray2);

<span class="hljs-comment">// Outcome </span>
<span class="hljs-literal">false</span>
</code></pre>
<h3 id="heading-some-only-if-you-want-to-know-truefalse-if-the-condition-meets-with-some-of-the-elements-in-an-array"><code>some()</code>: Only if you want to know true/false if the condition meets with some of the elements in an array</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> filteredArray3 = dogsDetails.some(<span class="hljs-function"><span class="hljs-params">dog</span> =&gt;</span> dog.age &gt; <span class="hljs-number">0.5</span>);
<span class="hljs-built_in">console</span>.log(filteredArray3);

<span class="hljs-comment">// Outcome </span>
<span class="hljs-literal">true</span>
</code></pre>
<h3 id="heading-find-only-if-you-want-to-return-the-first-element-that-meets-the-condition"><code>find()</code>: Only if you want to return the first element that meets the condition</h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> filteredArray4 = dogsDetails.find(<span class="hljs-function"><span class="hljs-params">dog</span> =&gt;</span> dog.color === <span class="hljs-string">'black'</span>);
<span class="hljs-built_in">console</span>.log(filteredArray4);

<span class="hljs-comment">// Outcome </span>
{<span class="hljs-attr">type</span>: <span class="hljs-string">"golden retriever"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>}
</code></pre>
<h2 id="heading-summary">Summary</h2>
<p>We have looked into some of the most popular and used array methods. It's important that you know what is the best approach to handle your data and consider which one is the most efficient way to handle the array. </p>
]]></content:encoded></item></channel></rss>