<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mahin Ahmed</title>
    <description>The latest articles on DEV Community by Mahin Ahmed (@mahin19524).</description>
    <link>https://dev.to/mahin19524</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4004773%2Fbb1ca128-f3e9-4792-bfa9-50d00b006a61.png</url>
      <title>DEV Community: Mahin Ahmed</title>
      <link>https://dev.to/mahin19524</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mahin19524"/>
    <language>en</language>
    <item>
      <title>I Tried to Rebuild Array.pop()... Then JavaScript Humbled Me( Learning journal #2-Reference)</title>
      <dc:creator>Mahin Ahmed</dc:creator>
      <pubDate>Thu, 09 Jul 2026 09:54:20 +0000</pubDate>
      <link>https://dev.to/mahin19524/i-tried-to-rebuild-arraypop-then-javascript-humbled-me-learning-journal-2-reference-3n00</link>
      <guid>https://dev.to/mahin19524/i-tried-to-rebuild-arraypop-then-javascript-humbled-me-learning-journal-2-reference-3n00</guid>
      <description>&lt;p&gt;It's been 12 days since I started learning JavaScript and it made me a bit annoyed already 😅. I learned about array after completing iteration and loops. So I thought of a little challenge; Why not try to reverse engineer some of array's built-in methods?&lt;/p&gt;

&lt;p&gt;I started with reverse engineering &lt;code&gt;array.push()&lt;/code&gt; method. My thought process was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;we need to get the max index of the array&lt;/li&gt;
&lt;li&gt;we need to add 1 with the max index to get new max index&lt;/li&gt;
&lt;li&gt;then we declare a variable with the new max index&lt;/li&gt;
&lt;li&gt;then we put a value inside that variable which is then stored to the array
Thought of this way because non-existing index returns undefined, so variable not declared or emty value&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;So basically I was trying to get the last index of an array and then adding another index and assigning a value to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// push method&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;//    Index: 0,  1,  2,  3,  4&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addElementToArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;elementName&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// const arrayMaxIndex = arrayName.length - 1; &lt;/span&gt;
  &lt;span class="c1"&gt;// const newMaxIndex = arrayMaxIndex + 1&lt;/span&gt;
  &lt;span class="c1"&gt;// Those lines were unneccessary&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newMaxIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arrayName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
  &lt;span class="nx"&gt;arrayName&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newMaxIndex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;elementName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I was confident that I can do some more. So I immediately tried to reverse engineer the &lt;code&gt;array.pop()&lt;/code&gt; method. And this is where I literary sucked. I thought it this way:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;we will create a new array&lt;/li&gt;
&lt;li&gt;we will run loop and add the elements of our initial array here&lt;/li&gt;
&lt;li&gt;but we will add until the last index, that's it&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy1y541esesupnefazxko.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy1y541esesupnefazxko.png" alt="Visual Diagram of operation-flow" width="800" height="216"&gt;&lt;/a&gt;&lt;br&gt;
So I began with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;removeElementFromArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayName&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;postArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;iniArrayElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nx"&gt;postArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iniArrayElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;arrayName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;postArray&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code was working as expected. It declares an empty array (local scope) inside our function, reads all the elements of our &lt;code&gt;arrayName&lt;/code&gt; and adds element one-by-one until the last index. Then it simply assigns the new completed array to our main array, right? No, it doesn't. I was confused cause when i tried &lt;code&gt;console.log(arrayName)&lt;/code&gt; after the line &lt;code&gt;arrayName = postArray ;&lt;/code&gt;, it was working. But when i tried &lt;code&gt;console.log(array)&lt;/code&gt;[array = [1,2,3,4,5]], it gives &lt;code&gt;1,2,3,4,5&lt;/code&gt; not &lt;code&gt;1,2,3,4&lt;/code&gt;!!&lt;/p&gt;

&lt;p&gt;I was really frustrated because I was telling JavaScript to copy all the elements of postArray and assign it to arrayName which refers our main array but JavaScript was kinda betraying me. It was changing the value inside the function but not in the global array. JavaScript wasn't wrong at all! It was I who misinterpreted concepts. &lt;/p&gt;

&lt;p&gt;Then I did some research and realized my mistake. JavaScript values are generally grouped into Primitive and Object. When we use &lt;code&gt;=&lt;/code&gt; or assignment operator in a variable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it is Primitive data-type, then JavaScript simply copies the content of the right side variable/content and stores those data in a memory block and assigns the memory address to the variable. So when we write &lt;code&gt;let num = 2;&lt;/code&gt;, javaScript does:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftvze7u6qx6xgz0kihvp1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftvze7u6qx6xgz0kihvp1.png" alt="Flow of primitive" width="764" height="105"&gt;&lt;/a&gt;&lt;br&gt;
So even if we do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;only &lt;code&gt;y&lt;/code&gt; changes, because '=' in primitive copies the whole content and assigns it to a new memory block and gives the memory address to the variable. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it is an Object-type, then JavaScript doesn't copy the object itself. Instead, it copies the reference to that object. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwwx3tmmqj8w75mv65lrb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwwx3tmmqj8w75mv65lrb.png" alt="Memory pointer" width="510" height="334"&gt;&lt;/a&gt;&lt;br&gt;
So if we do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [5,6,7,8]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [5,6,7,8]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;push()&lt;/code&gt; is actually changing the memory block content which &lt;code&gt;arr2&lt;/code&gt; is pointing at, but &lt;code&gt;arr1&lt;/code&gt; is also pointing at the same address. So as both 'share' the same memory block content, the changes apply to both. But if we do &lt;code&gt;arr2 = [1,2,3]&lt;/code&gt; then the engine will actually create a new memory block and assigns its address to &lt;code&gt;arr2&lt;/code&gt; and the changes will be only applicable to &lt;code&gt;arr2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That is why when i tried &lt;code&gt;arrayName = postArray&lt;/code&gt;, I was actually telling JavaScript:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hey, please change the &lt;code&gt;arrayName&lt;/code&gt; reference so it points to &lt;code&gt;postArray&lt;/code&gt; address. And the fun fact is that when we say&lt;code&gt;removeElementFromArray(array)&lt;/code&gt; then JavaScript just tells the engine to make the local variable &lt;code&gt;arrayName&lt;/code&gt; point to the same address where the data of global variable &lt;code&gt;array&lt;/code&gt; points. We can see this in action:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayname&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;arrayname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hah&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayname&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [ 1, 2, 3, 4, 5, 'hah' ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// [ 1, 2, 3, 4, 5, 'hah' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the changes applies to both cause both is pointing at the same memory address reference. So what was wrong with our function &lt;code&gt;removeElementFromArray&lt;/code&gt;? Because I was telling JavaScript to change the memory address of &lt;code&gt;arrayName&lt;/code&gt; to the address which refers to &lt;code&gt;postArray&lt;/code&gt;. So I was not updating my &lt;code&gt;array&lt;/code&gt; at all. That is why the changes was visible inside the function as I was doing &lt;code&gt;console.log(arrayName)&lt;/code&gt; but was not visible as I was doing &lt;code&gt;console.log(array)&lt;/code&gt;. Built-in methods like  &lt;code&gt;push()&lt;/code&gt; actually &lt;strong&gt;modifies&lt;/strong&gt; the content, but &lt;code&gt;=&lt;/code&gt; actually &lt;strong&gt;changes&lt;/strong&gt; what the variable is referring. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lesson:&lt;/strong&gt; I have to make sure I keep the concent of reference in mind while working with data.&lt;br&gt;
The biggest realization:&lt;/p&gt;

&lt;p&gt;= NEVER modifies an object.&lt;/p&gt;

&lt;p&gt;= NEVER modifies a primitive.&lt;/p&gt;

&lt;p&gt;= NEVER modifies ANY value.&lt;/p&gt;

&lt;p&gt;It ONLY changes what the variable on the LEFT refers to.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How I then approaches the problem?&lt;/strong&gt;&lt;br&gt;
A function cannot change which object the caller's variable refers to, but it can mutate the object that both the caller and the parameter currently reference.&lt;br&gt;
I understood that I can't reliably change the global array permanently, rather I can return the changed array and do the rest of the operations with this. But I went deeper and learned about objects also. And then I realized 'every list is essentially an object which has a built-in property called &lt;code&gt;length&lt;/code&gt;'. &lt;/p&gt;
&lt;h2&gt;
  
  
  Array
&lt;/h2&gt;

&lt;p&gt;0 : 10&lt;br&gt;
1 : 20&lt;br&gt;
2 : 30&lt;br&gt;
3 : 40&lt;br&gt;
length : 4&lt;/p&gt;

&lt;p&gt;Important:&lt;/p&gt;

&lt;p&gt;length is just another property of the array.&lt;/p&gt;

&lt;p&gt;So I thought of why not access the property &lt;code&gt;length&lt;/code&gt; and change it to &lt;code&gt;currentLength - 1&lt;/code&gt;? And I did it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;removeElementFromArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arrayName&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;arrayName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arrayName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arrayName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// returning Just for testing&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, JavaScript isn't strange. I was the one who didn't understand how it works. I love solving this types of problems. Facing such problems always makes me learn something new or understand the system better. And I love these sufferings 🙃.&lt;/p&gt;

&lt;p&gt;Thanks for taking the time to read this. I really appreciate you cause as a learner, I need people to help me go in the right direction and learn properly. My assumptions might be wrong, or maybe I have explained something in a wrong way or used wrong words. Hoping that you will provide some helpful feedback. And please let me know what mistakes I am still doing. &lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>I thought I understood JavaScript scopes... until this happened ( Learning journal #1-Variables and Data Types)</title>
      <dc:creator>Mahin Ahmed</dc:creator>
      <pubDate>Sat, 27 Jun 2026 09:14:37 +0000</pubDate>
      <link>https://dev.to/mahin19524/i-thought-i-understood-javascript-scopes-until-this-happened-learning-journal-1-variables-and-20m3</link>
      <guid>https://dev.to/mahin19524/i-thought-i-understood-javascript-scopes-until-this-happened-learning-journal-1-variables-and-20m3</guid>
      <description>&lt;p&gt;Actually, I was learning the fundamentals of programming in JavaScript. I was studying &lt;strong&gt;Variables and Data Types&lt;/strong&gt; where i learnt about &lt;strong&gt;scopes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I understood &lt;strong&gt;global&lt;/strong&gt; and &lt;strong&gt;block&lt;/strong&gt; scope variables. We usually declare a block scope variable with &lt;code&gt;{...}&lt;/code&gt; and the variable is only accessible from inside the brackets. If we try working with it outside the brackets then it will throw some errors. &lt;/p&gt;

&lt;p&gt;I was thinking okey thats good cause it seems simple right? Until I stumbled upon an interesting problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;serviceName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Auth_Service&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;localTimeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// [Checkpoint A] What are the values of serviceName and totalRequests here?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// [Checkpoint B] What happens if we try to console.log(localTimeout) here?&lt;/span&gt;
&lt;span class="c1"&gt;// [Checkpoint C] What is the value of totalRequests here?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I was confused cause we declared the same variable &lt;code&gt;totalRequests&lt;/code&gt; twice. Once in the global scope and once in the block scope. Shouldn't it throw an error of variable double-declaration?&lt;/p&gt;

&lt;p&gt;Then I thougt okey if it doesn't throw an error then it might work, but how? I know we can still access the global variable inside block scope variable. So it might just rewrite the &lt;code&gt;totalRequests&lt;/code&gt; value from 100 to 5, then we are adding 1 with it, so it should be 6. And it did.&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Checkpoint A&lt;/span&gt;
&lt;span class="nx"&gt;serviceName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Auth_Service&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;

&lt;span class="c1"&gt;// Checkpoint B&lt;/span&gt;
&lt;span class="nx"&gt;ReferenceError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;localTimeout&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;defined&lt;/span&gt;

&lt;span class="c1"&gt;// Checkpoint C&lt;/span&gt;
&lt;span class="nx"&gt;totalRequests&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But after trying to reason through it myself, I searched for an explanation and found this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;because your second let totalRequests = 5; was inside a { ... } block, the engine treats that block as a completely isolated room (a new Lexical Environment).&lt;/p&gt;

&lt;p&gt;When the engine is inside that block and looks for totalRequests, it checks its immediate room first. It finds the local one, uses it, and ignores the outer one. This is a real engineering concept called Variable Shadowing—the inner variable literally casts a shadow over the outer one, hiding it from view. Once the block ends and the room is destroyed, the shadow lifts, and the global totalRequests (which was never touched or modified) is visible again.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I interpreted the explaination as ,"Block scope prioritizes its own local variable first, so even if there is one already declared globally, it will not count it."&lt;/p&gt;

&lt;p&gt;I am a beginner who is learning JavaScript for the first time and I am currently focusing on strengthening my fundamentals rather than blindly choosing a trending framework. So it was a great funny challenge for me to solve. Though i successfully solved it but my assumptions were incorrect. Maybe I still haven't fully grasped the exact reason yet, if someone is kind enough to help this nerd, I would be too happy to learn it better. &lt;/p&gt;

&lt;p&gt;Thanks for reading this. If I've misunderstood something, I'd genuinely appreciate corrections. I'm documenting my learning journey, and I'd love to improve my understanding. I heard dev community is helpful and generous, I hope someone would help me clear out the confusion I have 😊.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>learning</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
