<?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 Grzegorz Zadora on Medium]]></title>
        <description><![CDATA[Stories by Grzegorz Zadora on Medium]]></description>
        <link>https://medium.com/@grzegorz.adam.zadora?source=rss-66a4e7606e69------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*UxQi1LkivwepH0BN5W9VBw.jpeg</url>
            <title>Stories by Grzegorz Zadora on Medium</title>
            <link>https://medium.com/@grzegorz.adam.zadora?source=rss-66a4e7606e69------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 22:26:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@grzegorz.adam.zadora/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[Explaining arrays in JS (common quirks and misconceptions)]]></title>
            <link>https://blog.codequest.com/explaining-arrays-in-js-common-quirks-and-misconceptions-9e38125de039?source=rss-66a4e7606e69------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e38125de039</guid>
            <category><![CDATA[array-methods]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Grzegorz Zadora]]></dc:creator>
            <pubDate>Tue, 09 Jun 2020 14:02:51 GMT</pubDate>
            <atom:updated>2025-02-28T13:11:49.772Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AqpQvKGo4ui0I20CsmtuIw.png" /><figcaption>img by <a href="https://medium.com/u/49cba4a72315">Mareta Mara</a></figcaption></figure><p>When you learn how to use arrays you will become a powerful developer. Some weird problems may stand on your way, though. So I decided to explain the general concepts of the array and some quirks which cause headaches even among experienced JS devs. I hope it will help you understand arrays better!</p><h3>What is an array?</h3><p>An array is a commonly used data structure. In simple words, it’s an ordered list of elements (values or variables), where each one is identified by a positive integer or zero (the first item’s index is always zero). However, you should be aware that JavaScript does not restrict you to this definition (see “Array in JavaScript is mutable” section). An array is great for storing a collection of items of any kind. You might want to use it to create a list of to-dos or employees in your company. Furthermore, it makes executing repeatable tasks really convenient, which allows us to solve problems such as sorting or finding a specific item in the collection.</p><h3>How to create an array?</h3><p>There are multiple ways of creating an array in JavaScript, here are some examples that come to my mind at the moment:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce7dca72e19211cfcfdf542201c26bf0/href">https://medium.com/media/ce7dca72e19211cfcfdf542201c26bf0/href</a></iframe><p>A convenient way to check the type of a particular value or variable is by using <strong><em>typeof</em></strong> keyword:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bac2a43b1f4bf1091edc4e313e249da9/href">https://medium.com/media/bac2a43b1f4bf1091edc4e313e249da9/href</a></iframe><p>However, when you try to use it on an array, a strange thing happens:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae10456e323b0116465cb50bea5aa224/href">https://medium.com/media/ae10456e323b0116465cb50bea5aa224/href</a></iframe><p>That’s because, under the hood, an array in JavaScript is indeed an object. It&#39;s not immediately obvious though, because you can create it using an array literal (square brackets).</p><h3>Array in JavaScript is mutable</h3><p>Programmers use the word “mutable” to sound smarter but the concept is simple: if something is mutable it can be modified. As mentioned above, JavaScript treats every array as an object. It is possible to add custom properties to arrays, just as with plain objects. It’s strongly discouraged but you could do that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a59268c6c037deff29e70c7f6ef6bb3/href">https://medium.com/media/6a59268c6c037deff29e70c7f6ef6bb3/href</a></iframe><p>By doing so you should not expect built-in array methods and properties to work correctly, though. It is nobody’s fault that you messed up your array ¯\_(ツ)_/¯.</p><p>To avoid further modifications on the array you might want to use the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze"><strong><em>Object.freeze</em></strong></a><strong><em> </em></strong>function on it. It will, however, make it impossible to use methods like <strong><em>.pop</em></strong> or <strong><em>.push</em></strong>. Also, it still allows you to modify objects inside the array so if you are really concerned about the mutability, check out <a href="https://immutable-js.github.io/immutable-js">Immutable.js</a> library.</p><h3>Length of an array is not fixed</h3><p>That means when you create an array you don’t care about its length. You might create an array which initially contains only four elements, but it’s not a problem to add more elements or delete some later. While you are adding or removing items from the array JavaScript automatically changes its <strong><em>.length </em></strong>property. It works properly only when you’re using standard built-in array methods such as <strong><em>.pop</em></strong> or <strong><em>.shift</em></strong>. When you try to delete an array element manually using the delete operator, .length won’t be updated:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/746565349701e487ac07f9c2335ad083/href">https://medium.com/media/746565349701e487ac07f9c2335ad083/href</a></iframe><h3>Length of an array is not infinite</h3><p>You should remember that an array’s length is not unlimited and a RangeError might occur when your array gets massive. The maximal size of an array is 4,294,967,295 elements (according to ECMA-262, 10th edition, tested on Chrome 80):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9bfd25a75c1853611106e3f8d2bfca05/href">https://medium.com/media/9bfd25a75c1853611106e3f8d2bfca05/href</a></iframe><h3>An array can contain different data types</h3><p>For example, consider the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eae573f396479f945860c60972183bcb/href">https://medium.com/media/eae573f396479f945860c60972183bcb/href</a></iframe><p>In the above snippet, you can see an array that contains: a string, a number, and a function that returns a string. Nothing can stop you from adding different data types to a single array. That doesn’t mean you should do that, though. In my opinion, it might make your code confusing. People might not expect strings, integers, and functions in the same array. However, if it’s really necessary, you have that capability.</p><h3>Multidimensional arrays</h3><p>Sometimes it’s not so obvious how to create and access elements of multidimensional arrays in JavaScript. To create a multidimensional array you need to nest arrays — one inside another. Basically you’re creating an array of arrays.</p><p>Let’s say you want to create a tic-tac-toe game. You need to implement a two-dimensional board. One way to approach the problem is by using a two-dimensional array, in JavaScript, you would do the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55bde9a624885f638b786c0d5dc0ae9f/href">https://medium.com/media/55bde9a624885f638b786c0d5dc0ae9f/href</a></iframe><h3>Spread operator</h3><p>It’s syntactic sugar which can replace <strong><em>.from</em></strong> and <strong><em>.concat</em></strong> methods. It allows us to copy or merge arrays:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19f091df88eb07bf5bb3c1a6e91ed112/href">https://medium.com/media/19f091df88eb07bf5bb3c1a6e91ed112/href</a></iframe><p>Another use case is to create an array from a string</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6490d61151675b52d0d577ecafce1bd9/href">https://medium.com/media/6490d61151675b52d0d577ecafce1bd9/href</a></iframe><p>It might also be useful when you want to pass all elements of an array as arguments to a function (that’s really useful in my opinion):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8114c76729250a16811cbb26ff828183/href">https://medium.com/media/8114c76729250a16811cbb26ff828183/href</a></iframe><h3>Destructuring assignment</h3><p>We can grab some items from an array and immediately assign them to variables:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36617883a38eb2bd5aaa159378e0dc5f/href">https://medium.com/media/36617883a38eb2bd5aaa159378e0dc5f/href</a></iframe><p>Also, we can combine it with the spread operator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb5a8cd2f0bd5beba6555dd438dcb6ca/href">https://medium.com/media/cb5a8cd2f0bd5beba6555dd438dcb6ca/href</a></iframe><h3>Comparing arrays</h3><p>Comparing arrays is not so simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c9ea08ff104db53b9ff27821c278484/href">https://medium.com/media/2c9ea08ff104db53b9ff27821c278484/href</a></iframe><p>So what can we do here? We can implement a simple comparison ourselves:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3814ca42a5ed150d2b9befbfa822da3/href">https://medium.com/media/c3814ca42a5ed150d2b9befbfa822da3/href</a></iframe><p>It’s a naive solution and it works only for primitive values because we are executing shallow comparisons here (we are comparing by value). It will not work when we have non-primitive values in the array (e.g. objects). In this case, you need to implement a deep comparison. It’s not a trivial task, though. In such cases, to avoid reinventing the wheel, <a href="https://lodash.com/docs/4.17.15#isEqual">I recommend using isEqual from the lodash library</a>.</p><h3>Passing an array to the function</h3><p>When you pass an array as a function argument you might obtain bizarre results. Look at the snippet below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05e61d4ea056b39484fc04e34a0ed7b6/href">https://medium.com/media/05e61d4ea056b39484fc04e34a0ed7b6/href</a></iframe><p>Hm, the array has been changed inside the function… What can we do about it? Maybe we can assign the array to a local variable inside the function? Let’s try it…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbfda817d7ef410a0b71fa36ef5e68d2/href">https://medium.com/media/fbfda817d7ef410a0b71fa36ef5e68d2/href</a></iframe><p>Still, we have the same result… That’s because when we deal with an object (and as I previously mentioned, the array is one of a special kind), the variable only keeps a reference to that object, not the object itself. So, regardless of whether we pass a variable to the function or use a local variable inside of it, we’re still dealing with the same array. What we need to do is create a copy of the array, so that we can safely work on it inside the function and still have the original array outside the function.</p><p>Let’s try it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68e90239ad0867ad9c33cdead6b6d91c/href">https://medium.com/media/68e90239ad0867ad9c33cdead6b6d91c/href</a></iframe><p>It works! I’m aware this part might be confusing. To understand it deeply please look up: Value vs. Reference in JavaScript.</p><h3>Mutating the array</h3><p>It’s not good practice to manipulate an array directly because it will be difficult to track the changes we’ve made to it, either for the JS engine and people who read the code. Instead, we should use array mutating methods. Most of them are pretty straightforward so I’m not going to cover all of them in details, but rather just give a quick overview:</p><ul><li><strong>[].pop - </strong>removes the last element from an array and return this element</li><li><strong>[].push - </strong>adds one or more elements to the end of an array and returns the new length of the array</li><li><strong>[].unshift - </strong>adds one or more elements to the beginning of an array and returns the new length of the array</li><li><strong>[].shift -</strong> removes the first element from an array and returns this element</li><li><strong>[].reverse - </strong>reverse the order of the elements in the array</li></ul><p>More knowledge is available <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">on the MDN</a>. However, there is one method that is not so simple to understand and I’d like to discuss it…</p><h3>[ ].splice(start, deleteCount, …items)</h3><p><strong>start</strong> — the position from which you want to start removing items</p><ul><li>If it’s greater than array length, it is set to be equal to the array’s length and no items will be deleted</li><li>If it’s smaller than 0 it will start from the <strong>position = array.length — start</strong></li><li>If <strong>array.length</strong> + <strong>start</strong> &lt; 0 it is set to 0</li></ul><p><strong>deleteCount</strong> — stands for the number of items you want to delete</p><ul><li>If is omitted or greater or equal to array.length, then all of the array elements are removed</li></ul><p><strong>…items </strong>— after start and deleteCount you can put (as many as you want) values which you want to add to the array after the start position</p><p><strong><em>Splice</em></strong> returns<strong> </strong>an array of items which have been removed from the calling array or empty array if none of the items were removed. You can use<strong> </strong>it when you want to add, delete or replace items at a specific position in the array</p><p>There is a big chance that you’ve heard that it’s good practice to ensure that a function does only one thing and does it well. Splice doesn’t fit into this rule. You can use it for two opposite things: adding and deleting elements to and from an array, even simultaneously. Furthermore, this method’s name is almost identical to <strong><em>.slice</em></strong>, despite the fact that they work in a completely different way. It’s also important to remember that this method always returns an array (even if just one item has been removed or none of the items have been removed). Some use cases:</p><ul><li>Remove element at a specific index:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c5dc33598c437596757510df0f24617/href">https://medium.com/media/5c5dc33598c437596757510df0f24617/href</a></iframe><ul><li>Add element at a specific index:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f519086ef8e89008d43e23b3b588a85/href">https://medium.com/media/9f519086ef8e89008d43e23b3b588a85/href</a></iframe><ul><li>Replace element at a specific index:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5eabe03c1c6dae92bb30f4e26cba526e/href">https://medium.com/media/5eabe03c1c6dae92bb30f4e26cba526e/href</a></iframe><h3>Iterating over array elements</h3><p>What makes arrays really useful is that we can easily iterate over their elements. That allows you to do repeatable tasks with a small amount of code. There are many ways of doing that so it might be a little bit confusing. Let’s see how they differ:</p><ul><li><strong>Classic “for” loop<br></strong>Mostly used by the beginners since it’s described in most of the programming books. In the example below, you can see how to <strong><em>console.log</em></strong> each element of the array using for loop.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1be0d7f405f6a2e607529fb92db3df0/href">https://medium.com/media/e1be0d7f405f6a2e607529fb92db3df0/href</a></iframe><p>It’s a pretty straightforward approach. It has some problems, though:</p><ul><li>It’s not so convenient to declare a variable for an index, a condition, and an action each time you want to iterate over something. In bigger code bases it would be repeated a lot.</li><li>It leaves room for so-called “off-by-one” errors. Sometimes people might not be sure if it will repeat 2 times, or 3 times or 4 times… Maybe someone accidentally put &lt;= instead of &lt;.</li></ul><p>It might not sound like a big deal, but you know — code smells…</p><ul><li><strong>For…of<br></strong>A better version of the above. You don’t have to declare a variable and a condition, so you won’t get off-one errors anymore.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96593a5461f4526776dbcd57b79e2038/href">https://medium.com/media/96593a5461f4526776dbcd57b79e2038/href</a></iframe><ul><li><strong>For…in loop<br></strong>The problem with the previous approach occurs when you need an element’s index for some reason. Then you can use for..in loop which is very similar, but in each iteration, you’ll get an element’s index instead of the element itself.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83733ccb6112706f45e2a604e4d52ec3/href">https://medium.com/media/83733ccb6112706f45e2a604e4d52ec3/href</a></iframe><ul><li><strong>Array iteration methods<br></strong>For dessert, we have array iteration methods. There are a few of them and each one is slightly different, but the rule is the same for all: you put a function (usually called a callback) as the argument of the iteration method and that function will be called for each element of the array. It’s a more sophisticated way of iterating over array elements than previous ones in my opinion. Since there are iteration methods for different cases you make your intent more clear for the reader. What’s also good, you’ll always get at least two parameters in the callback function: the current element and its index.</li></ul><p>Let’s try the most basic one, forEach</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b45550e75b9322ce15d22b5503a02d51/href">https://medium.com/media/b45550e75b9322ce15d22b5503a02d51/href</a></iframe><p>There is one gotcha with this approach, however:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93e03ef88857732848a4c545ad658d57/href">https://medium.com/media/93e03ef88857732848a4c545ad658d57/href</a></iframe><h3>Why can’t we iterate over the array created using the constructor?</h3><p>As you’ve seen above <strong><em>forEach</em></strong> (as well as <strong><em>map</em></strong>, <strong><em>filter</em></strong>, and other iteration methods) won’t work with the array created using the constructor. Let’s do a little investigation. First. let’s see what’s inside array created using literal:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2c7a731684f62d9cf21678f777b8460/href">https://medium.com/media/a2c7a731684f62d9cf21678f777b8460/href</a></iframe><p>The result will be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/246/0*td3YtFCBqc9PA5yZ" /></figure><p>If you take a look at the array created using constructor you can see something different:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1021ac45d49720dd13c961539d5fd1ec/href">https://medium.com/media/1021ac45d49720dd13c961539d5fd1ec/href</a></iframe><p>The result will be:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/232/0*t1bUE-3ZdCTghniW" /></figure><p>It seems that the current array does have the desired length, although it doesn’t have any elements. Since it doesn’t have any elements there is nothing to iterate through. The iteration method has been designed to iterate over existing array indexes. That way we can avoid running unnecessary iterations on nonexisting items.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/174410e114479700d65eecf6f5d32b94/href">https://medium.com/media/174410e114479700d65eecf6f5d32b94/href</a></iframe><p>So the easy fix for the constructed array will be for instance using .<strong><em>fill</em></strong> method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48583674bd2f280919f7132634636502/href">https://medium.com/media/48583674bd2f280919f7132634636502/href</a></iframe><h3>Summary</h3><p>When you are aware of some counter-intuitive features of JavaScript, you can avoid most of the common issues. You might wonder: why won’t we fix those parts of the language in its further versions? It’s not so easy. We have to support legacy code and that’s why we cannot just change or remove anything we want from the language… However, if you remember about those little quirks, you can save yourself a lot of headaches.</p><p>💌 <strong>You like stories like this? Don’t miss out — </strong><a href="https://medium.com/codequest/newsletters/codequest"><strong>join our newsletter!</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e38125de039" width="1" height="1" alt=""><hr><p><a href="https://blog.codequest.com/explaining-arrays-in-js-common-quirks-and-misconceptions-9e38125de039">Explaining arrays in JS (common quirks and misconceptions)</a> was originally published in <a href="https://blog.codequest.com/">codequest</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>