<?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[devsnb]]></title><description><![CDATA[devsnb]]></description><link>https://blog.devsnb.com</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 05:48:01 GMT</lastBuildDate><atom:link href="https://blog.devsnb.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Beginners Guide To JavaScript Arrays]]></title><description><![CDATA[What is an Array?
It is an extremely important data structure not just in JavaScript but also in many other programming languages. Arrays let us store list of values in a single variable that's in order, can be mutated and more importantly iterated t...]]></description><link>https://blog.devsnb.com/beginners-guide-to-javascript-arrays</link><guid isPermaLink="true">https://blog.devsnb.com/beginners-guide-to-javascript-arrays</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[array]]></category><category><![CDATA[Beginner Developers]]></category><dc:creator><![CDATA[Snehangshu Biswas]]></dc:creator><pubDate>Tue, 14 Dec 2021 17:06:15 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1639335777391/jQG6Za1L9.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-an-array">What is an Array?</h2>
<p>It is an extremely important data structure not just in JavaScript but also in many other programming languages. Arrays let us store list of values in a single variable that's in order, can be mutated and more importantly iterated through.</p>
<h2 id="heading-how-to-create-an-array">How to create an array?</h2>
<p>Before we discuss about how to create an Array let's start by asking the question why do we need an array in the first place.</p>
<p>Let's assume you have a few hobbies, "Gardening", "Fishing", "Gaming". Now, if you want to store them in your program you have two choices. You can store each of your hobbies in a separate variable.</p>
<pre><code class="lang-plaintext">let hobby1 = "Gardening"
let hobby2 = "Fishing"
let hobby3 = "Gaming"
</code></pre>
<p>The second option would be to store them all inside of a single Array which is the preferred way.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">"Gardening"</span>, <span class="hljs-string">"Fishing"</span>, <span class="hljs-string">"Gaming"</span>]
</code></pre>
<p>So, what's the problem with creating separate variables? In the programming world actually it's pretty obvious. Let's understand this.</p>
<p>Assume you've developed a new hobby over the time, then you have to come back to your code and declare a new variable and store your precious new hobby. And you have to do the same every time you develop a new hobby. Meaning you're repeating the same thing again and again.</p>
<p>Okay, I hope everyone understands why we need arrays now.</p>
<p>So, how do we actually create an array? Well, there are a few different ways you can but we're going to discuss the most basic way to do so which is using a <code>[]</code> as we've already seen in the previous code snippet.</p>
<p>There our array already had a few values inside. So, if you're planning on creating an empty array you can do so by:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> myArray = [];
</code></pre>
<p>Okay, finally we know how to create an array in javascript.</p>
<p>Now we've seen how to create an array with and without values but how do we add or remove a value from an existing array? We have a few simple methods to do that.</p>
<p><code>Array push(), pop(), shift(), unshift()</code></p>
<p>Wait, wait, wait a minute!🤚 Before we try to understand about those pretty interesting methods let's get to know a few other important topics related to Arrays: Array <code>length</code> and Array <code>index</code>.</p>
<p><strong>Note:</strong> You can store any datatype in arrays, it can be String, number, other arrays, objects etc. And because of JavaScript's extremely dynamic and forgiving nature we can mix match datatypes in Arrays.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> myArray = [<span class="hljs-string">"Programming"</span>, <span class="hljs-number">106</span>, [<span class="hljs-string">"Hi guys"</span>], {<span class="hljs-attr">name</span>: <span class="hljs-string">"Alex"</span>}]

<span class="hljs-built_in">console</span>.log(myArray)
</code></pre>
<p>If you've not notice I've stored String, number, another array and even an object inside of the array <code>myArray</code>. It is completely valid in JavaScript.</p>
<h2 id="heading-array-length">Array Length</h2>
<p>So, what does length mean? I hope everyone understands what <code>length</code> means in plain English. So, what does it mean in programming?</p>
<p>The <code>number of elements</code> in an Array is the length of that particular Array.</p>
<p>Let's see a few examples.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">5</span>, <span class="hljs-number">9</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">2</span>];
</code></pre>
<p>For the above Array <code>length</code> is <code>6</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">"Gardening"</span>, <span class="hljs-string">"Fishing"</span>, <span class="hljs-string">"Gaming"</span>]
</code></pre>
<p>For the above Array <code>length</code> is <code>3</code>.</p>
<p>So, what would be the length for an <code>empty</code> Array? It's <code>0</code>(zero).</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = []
</code></pre>
<p>For the above Array <code>length</code> is <code>0</code>.</p>
<p>So, how do we calculate the length of an array? Mostly we don't calculate the length ourselves because in JavaScript the length is already there as a property in the Array. To get the value we simply use the <code>length</code> property.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">"Gardening"</span>, <span class="hljs-string">"Fishing"</span>, <span class="hljs-string">"Gaming"</span>]

<span class="hljs-built_in">console</span>.log(hobbies.length)
</code></pre>
<p>The above snippet prints <code>3</code> in the console. So, the <code>.length</code> property is going to return the length of that particular Array at that time.</p>
<h2 id="heading-array-index">Array Index</h2>
<p>Okay, enough of this Array length. Let's talk about another extremely important topic for Array, <code>index</code>. So, what is an index? The index in array refers to the the <code>position</code> of an element in an array.</p>
<p>Let's say you're living in a shared apartment. You have your own room and that room has a room number. So, you and the other residents in the apartment are the <code>elements</code> and the room numbers are the <code>index</code>. So, if I have to go to your room I can do so by only knowing the apartment name(variable) and the room number(index) you are living in instead of going to every room looking for you.</p>
<p>The maximum Index number in an array will be <code>(length of an Array - 1)</code>. Simply because they start from <code>0</code>(zero). Meaning index numbers in Array is zero based.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> fruits = [<span class="hljs-string">'Banana'</span>, <span class="hljs-string">'Apple'</span>, <span class="hljs-string">'Orange'</span>, <span class="hljs-string">'Pineapple'</span>];
<span class="hljs-comment">// Index :       0         1         2          3</span>
</code></pre>
<p>Here, the <code>length</code> of our Array is 4. So, the maximum the index number can go upto is 3(length of Array - 1). So, what are the index numbers for our array elements?</p>
<p>Indexing elements starts from the first element in an array from the left, so in our example <code>Banana</code> is the first element and as I said before the index number starts from <code>0</code>(zero). So, the index for banana is 0.</p>
<p>Then index number is incremented by 1 for every next element in the Array. So, the other index numbers are: <code>Apple = 1</code>, <code>Orange = 2</code>, <code>Pineapple = 3</code>.</p>
<p>As we can see the index number stopped at pineapple with 3 which is the last element in the Array. So, we can conclude(I'm the only one 😔 making these conclusions) whatever I said earlier was true.</p>
<p>Oh boy, a lot of things are here. If you feel overwhelmed please feel free to take s break and come back later. 😉</p>
<h2 id="heading-arrays-work-in-lifo-order">Arrays work in LIFO order</h2>
<p>So, what does it mean? Specifically what does <code>LIFO</code> means.</p>
<p><code>LIFO = Last In, First Out</code></p>
<p>Don't worry it's just a fancy term in programming that means the item that gets in the array last is the first item that can get out of it. Meaning, we can only add and remove elements from the end on an Array.</p>
<p>For now, just know this term we'll explore and see examples in detail very soon.</p>
<p>Now, comes the interesting part 😉. How to mutate an Array? Specifically, how to add or remove elements from an array?</p>
<h2 id="heading-array-push-method"><code>Array push()</code> Method</h2>
<p>Using the <code>Array push()</code> method we can add an element at the very end of an Array.</p>
<p>It follows the <code>LIFO</code> principle.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">'Cycling'</span>, <span class="hljs-string">'Gardening'</span>, <span class="hljs-string">'Swimming'</span>]

hobbies.push(<span class="hljs-string">'Fishing'</span>)

<span class="hljs-built_in">console</span>.log(hobbies)
</code></pre>
<p>This is going to add <code>Fishing</code> at the end of the the <code>hobbies</code> Array and mutate it into:</p>
<p><code>['Cycling', 'Gardening', 'Swimming', 'Fishing']</code></p>
<p>Easy right? 😁</p>
<h2 id="heading-array-pop-method"><code>Array pop()</code> Method</h2>
<p><code>Array pop()</code> method does the exact opposite of our previous method <code>Array push()</code>. Meaning it removes the last element from an Array.</p>
<p>It follows the <code>LIFO</code> principle.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">'Cycling'</span>, <span class="hljs-string">'Gardening'</span>, <span class="hljs-string">'Swimming'</span>]

hobbies.pop()

<span class="hljs-built_in">console</span>.log(hobbies)
</code></pre>
<p>The above snippet will remove <code>Swimming</code> from the end of the <code>hobbies</code> Array and mutate it into:</p>
<p><code>['Cycling', 'Gardening']</code></p>
<p>Also, the <code>Array pop()</code> method returns the element it removed from the Array.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">'Cycling'</span>, <span class="hljs-string">'Gardening'</span>, <span class="hljs-string">'Swimming'</span>]

<span class="hljs-keyword">let</span> removedHobby = hobbies.pop()

<span class="hljs-built_in">console</span>.log(removedHobby)
</code></pre>
<p>The above snippet will print <code>Swimming</code> in the console.</p>
<p>Pretty Interesting right? I know. If for some reason if you think it's too much just take a break and come back to it later.</p>
<p>Now, we're going to look at two more methods related to Array.</p>
<h2 id="heading-array-unshift-method"><code>Array unshift()</code> Method</h2>
<p>The <code>Array unshift()</code> works similar to the <code>Array push()</code> method but the main difference is: it adds the element to the front of the Array instead of the end.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">'Cycling'</span>, <span class="hljs-string">'Gardening'</span>, <span class="hljs-string">'Swimming'</span>]

hobbies.unshift(<span class="hljs-string">'Fishing'</span>)

<span class="hljs-built_in">console</span>.log(hobbies)
</code></pre>
<p>The above code snippet will add <code>Fishing</code> at the front of the the <code>hobbies</code> array and mutate it into:</p>
<p><code>['Fishing', 'Cycling', 'Gardening', 'Swimming', 'Fishing']</code></p>
<p>As expected the <code>Array .unshift()</code> method has added the element to the front of the <code>hobbies</code> Array.</p>
<p><strong>Note:</strong> It might look very tempting to use this method to insert an element to the front of an array but it's not recommended at all as it affects the performance of our code.</p>
<h2 id="heading-array-shift-method"><code>Array shift()</code> Method</h2>
<p>Well <code>Array shift()</code> does everything every similar to the previous <code>Array pop()</code> method but it removes the element from the front of the Array.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">'Cycling'</span>, <span class="hljs-string">'Gardening'</span>, <span class="hljs-string">'Swimming'</span>]

hobbies.shift()

<span class="hljs-built_in">console</span>.log(hobbies)
</code></pre>
<p>This will remove <code>Cycling</code> from the front of the the <code>hobbies</code> array and mutate it into:</p>
<p>` ['Gardening', 'Swimming']</p>
<p><code>When we use</code>Array shift()<code>it returns the removed element from the array just as the</code>Array pop()` does.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">'Cycling'</span>, <span class="hljs-string">'Gardening'</span>, <span class="hljs-string">'Swimming'</span>]

<span class="hljs-keyword">let</span> removedHobby = hobbies.shift()

<span class="hljs-built_in">console</span>.log(removedHobby)
</code></pre>
<p>The above code snippet will print <code>Cycling</code> to the console.</p>
<p><strong>Note:</strong> Just as the <code>Array unshift()</code> this <code>Array shift()</code> is not recommended to use as it costs quite a bit of performance in our code.</p>
<h2 id="heading-retrieve-element-from-a-specific-index">Retrieve Element From a Specific Index</h2>
<p>We first have to know the position of the value or to be precise the <code>Index number</code> of the element we want to retrieve from the Array. Then we attach the <code>[]</code> to the variable name and inside we pass the <code>Index number</code> of our desired element.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> hobbies = [<span class="hljs-string">'Cycling'</span>, <span class="hljs-string">'Gardening'</span>, <span class="hljs-string">'Swimming'</span>]
<span class="hljs-comment">// Index :         0           1            2</span>

<span class="hljs-keyword">let</span> hobby = hobbies[<span class="hljs-number">1</span>];
<span class="hljs-built_in">console</span>.log(hobby);
</code></pre>
<p>The above code snippet will print <code>Gardening</code> in the console which is the second element in our Array as we passed 1 inside the <code>[]</code>.</p>
<p>The index number starts from zero, remember?</p>
]]></content:encoded></item></channel></rss>