<?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[Sedat Can Yalçın's Blog]]></title><description><![CDATA[Hey, I'm Sedat a Software Engineer. This is my blog about Development using JavaScript and many others.]]></description><link>https://blog.sedat.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 21 Apr 2026 16:23:05 GMT</lastBuildDate><atom:link href="https://blog.sedat.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[JS Bits - Destructuring arrays in JavaScript!]]></title><description><![CDATA[Destructuring
Destructuring is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. (https://dev.to/sarah_chima/destructuring-assignment---arrays-16f)
Rather than getting a ...]]></description><link>https://blog.sedat.dev/js-bits-destructuring-arrays-in-javascript</link><guid isPermaLink="true">https://blog.sedat.dev/js-bits-destructuring-arrays-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[programming languages]]></category><category><![CDATA[arrays]]></category><category><![CDATA[Developer]]></category><category><![CDATA[learn coding]]></category><dc:creator><![CDATA[Sedat Can Yalçın]]></dc:creator><pubDate>Sat, 19 Sep 2020 14:09:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1600524487764/GGgLcxDPQ.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="destructuring">Destructuring</h1>
<p>Destructuring is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. (https://dev.to/sarah_chima/destructuring-assignment---arrays-16f)</p>
<p>Rather than getting a whole data, with destructuring we can only retrieve the values we want.</p>
<h2 id="destructuring-arrays">Destructuring arrays</h2>
<p>We have a function groceries which returns us our list of items that we wish to buy next time we go to a supermarket.</p>
<p>The data that groceries function returns is as follows; [bread, apples, cheese]</p>
<p>In a traditional sense we would get a reference to each item in this way;</p>
<pre><code><span class="hljs-keyword">const</span> groceriesList = groceries();
<span class="hljs-keyword">const</span> bread = groceriesList[<span class="hljs-number">0</span>];
<span class="hljs-keyword">const</span> apples = groceriesList[<span class="hljs-number">1</span>];
<span class="hljs-keyword">const</span> cheese = groceriesList[<span class="hljs-number">2</span>];
</code></pre><p>Destructuring allows us to achieve this in an elegant and simple way</p>
<pre><code><span class="hljs-keyword">const</span> [
    bread,
    apples,
    cheese
] = groceries()
</code></pre><p>If you also want a reference to the groceries list all you need to do is;</p>
<pre><code><span class="hljs-keyword">const</span> [
    bread,
    apples,
    cheese
] = groceriesList = groceries()
</code></pre><p>But, what happens if groceries returns us an array with four values?</p>
<p>Simply, we would only get the first three values without touching the fourth value.</p>
<p>What happens when we want to retrieve three values but groceries function returns two values?</p>
<p>Let's say the array doesn't have <strong>cheese value</strong> and we want to reference to this value with the <strong>cheese variable</strong>.</p>
<p>The code would not break and the cheese variable will simply be <strong>undefined</strong></p>
<h3 id="undefined-values">Undefined values</h3>
<p>Another case is when a value is undefined.</p>
<p>Imperatively if a value can be undefined and we want a default value to it when it's undefined.</p>
<p>We would do;</p>
<pre><code><span class="hljs-keyword">const</span> name = names[<span class="hljs-number">0</span>] !== <span class="hljs-literal">undefined</span> ? names[<span class="hljs-number">0</span>] : <span class="hljs-string">'unregistered'</span>
</code></pre><p>Declaratively, using destructuring we do;</p>
<pre><code>const [
<span class="hljs-type">name</span> = <span class="hljs-string">'unregistered'</span>
] = names
</code></pre><h3 id="what-if-we-want-to-get-the-first-three-into-their-own-variables-and-the-rest-into-one-variable">What if we want to get the first three into their own variables and the rest into one variable?</h3>
<p>In order to achieve this we use the spread operator.</p>
<pre><code><span class="hljs-keyword">const</span> [
    bread,
    apples,
    cheese
    <span class="hljs-comment">// get the first three as normal then</span>
    ...others <span class="hljs-comment">// gather the rest of them and put them into variable others</span>
] = groceries()
</code></pre><h3 id="variable-swapping-with-destructuring">Variable swapping with destructuring</h3>
<p>Destructuring allows a handy trick for swapping two variables without the need for a temp variable.</p>
<pre><code>[<span class="hljs-meta">x,y</span>] = [y,x]
</code></pre><h3 id="destructuring-function-paramaters">Destructuring function paramaters</h3>
<p>You can destructure an array which is a parameter to a function</p>
<pre><code><span class="hljs-string">const</span> <span class="hljs-string">arr</span> <span class="hljs-string">=</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-string">const</span> <span class="hljs-string">firstThree</span> <span class="hljs-string">=</span> <span class="hljs-string">([one</span> <span class="hljs-string">=</span> <span class="hljs-number">10</span><span class="hljs-string">,</span> <span class="hljs-string">two,</span> <span class="hljs-string">three])</span> <span class="hljs-string">=&gt;</span> {
    <span class="hljs-string">//</span> <span class="hljs-string">do</span> <span class="hljs-string">something</span>
}
</code></pre><p>The parameter 10 has a default value of 10 if it receives undefined</p>
<h3 id="destructuring-nested-arrays">Destructuring nested arrays</h3>
<p>We can destructure nested array using another pair of brackets inside our brackets</p>
<pre><code><span class="hljs-keyword">const</span> <span class="hljs-keyword">data</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-keyword">const</span> [
    one,
    [two, three] = [], <span class="hljs-comment">// fallback to an empty array if undefined</span>
    four

]
</code></pre><p>Thank you for reading! 
If you have any questions please let me know <a target="_blank" href="https://sedat.dev">here</a>!</p>
]]></content:encoded></item><item><title><![CDATA[JS Bits - Array Methods]]></title><description><![CDATA[Welcome to JS Bits
Hello everyone, welcome to the first post on my new series JS Bits where I explain and also show you use cases where you can use them. 
Array Methods
Array.prototype.find(callback)
Takes a callback function that returns the value o...]]></description><link>https://blog.sedat.dev/js-bits-array-methods</link><guid isPermaLink="true">https://blog.sedat.dev/js-bits-array-methods</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[programming languages]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Sedat Can Yalçın]]></dc:creator><pubDate>Wed, 16 Sep 2020 19:53:09 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1600286146400/Sb9kql2R8.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="welcome-to-js-bits">Welcome to JS Bits</h1>
<p>Hello everyone, welcome to the first post on my new series JS Bits where I explain and also show you use cases where you can use them. </p>
<h2 id="array-methods">Array Methods</h2>
<h3 id="arrayprototypefindcallback">Array.prototype.find(callback)</h3>
<p>Takes a callback function that returns the value of the first element that satisfies the condition.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">5</span>, <span class="hljs-number">12</span>, <span class="hljs-number">8</span>, <span class="hljs-number">130</span>, <span class="hljs-number">44</span>, <span class="hljs-number">130</span>];
<span class="hljs-keyword">const</span> found = arr.find(element =&gt; element &gt; <span class="hljs-number">50</span>);
<span class="hljs-comment">// found is equal to 130. It found the value 130 in the third index and returned it.</span>
</code></pre>
<h3 id="arrayprototypefindindexcallback">Array.prototype.findIndex(callback)</h3>
<p>Similar to find method, returns the index of the first value that satisfies the condition</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> arr = [<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> isLargeNumber = arr.findIndex((element) =&gt; element &gt; <span class="hljs-number">3</span>); <span class="hljs-comment">// return the index of value 4 which is 3</span>
</code></pre>
<h3 id="arrayprototypeincludesvaluetofind-fromindex">Array.prototype.includes(valueToFind[, fromIndex])</h3>
<p>Returns <code>true</code> or <code>false</code> whether the array includes the given value.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">3</span>,<span class="hljs-number">2</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>]
<span class="hljs-built_in">console</span>.log(arr.includes(<span class="hljs-number">5</span>)) <span class="hljs-comment">// returns true</span>
</code></pre>
<p>As an optional argument includes takes a parameter fromIndex which means where to begin the searching for the valueToFind</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> arr = [<span class="hljs-number">3</span>,<span class="hljs-number">2</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>]
<span class="hljs-built_in">console</span>.log(arr.includes(<span class="hljs-number">5</span>, <span class="hljs-number">2</span>)) <span class="hljs-comment">// starts searching the value 5 in arr beginning from the second index which returns true.</span>
</code></pre>
<h3 id="arrayprototypeflatdepth">Array.prototype.flat([depth])</h3>
<p>Creates a new flattend array with all the sub-array (nested parts) values taken out and concentinated in to the higher depth.</p>
<p>You will understand it better with this example;</p>
<pre><code class="lang-js"><span class="hljs-comment">// we have a nested array like this</span>
<span class="hljs-keyword">const</span> arr = [<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">6</span>, [[<span class="hljs-number">7</span>]] 
<span class="hljs-comment">// this array has [3,4] which goes one level deeper and empty array of [[]] which goes two levels deeper</span>
<span class="hljs-keyword">const</span> newArr = arr.flat() <span class="hljs-comment">// default value is one, returns the array: [1,2,3,4,5,6,[7]]</span>
<span class="hljs-keyword">const</span> otherARr = arr.flat(<span class="hljs-number">2</span>) <span class="hljs-comment">// goes to the depth of two and returns: [1,2,3,4,5,6,7]</span>
</code></pre>
<h3 id="arrayprototypeflatmapcallbackcurrentvalue-index-array">Array.prototype.flatMap(callback(currentValue[, index[, array]])</h3>
<p>It is very common now to use functional programming or FP methods in JavaScript like map().</p>
<p>If you want to use <code>flat()</code> on an array you are mapping. You have to first <code>map()</code>, which creates a new array and then call the <code>flat()</code> method.</p>
<p><code>flatMap()</code> combines a map and a flat with a <strong>depth of one</strong> by first mapping each element and then running flat on the newly created array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> arr = [<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-keyword">const</span> doubleArr = arr.flatMap(x =&gt; [x * <span class="hljs-number">2</span>]);
<span class="hljs-comment">// [2, 4, 6, 8]</span>
</code></pre>
<p>A very good use case of <code>flatMap()</code> is adding or removing items during a <code>map()</code>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> returnDoubles = [<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">6</span>].flatMap((v) =&gt; {
    <span class="hljs-keyword">if</span>(v % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>) {
        <span class="hljs-keyword">return</span> [v];
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> [];
    }
})
<span class="hljs-comment">// [2,4,6]</span>
</code></pre>
<p>Keep in mind that if you want to run <code>flatMap()</code> with a depth different than one(1). You have to call <code>flat()</code> additionally with a <code>map()</code>.</p>
<p>Want to learn more about me? Here's my <a target="_blank" href="https://sedat.dev">portfolio</a></p>
]]></content:encoded></item></channel></rss>