<?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: Bhumesh Mahajan</title>
    <description>The latest articles on DEV Community by Bhumesh Mahajan (@bhumesh_15).</description>
    <link>https://dev.to/bhumesh_15</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2343757%2F38bc0894-2e5d-49ac-820d-6b7f91c1b629.png</url>
      <title>DEV Community: Bhumesh Mahajan</title>
      <link>https://dev.to/bhumesh_15</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhumesh_15"/>
    <language>en</language>
    <item>
      <title>What's arr['1'] in JavaScript?</title>
      <dc:creator>Bhumesh Mahajan</dc:creator>
      <pubDate>Wed, 05 Feb 2025 18:11:00 +0000</pubDate>
      <link>https://dev.to/bhumesh_15/whats-arr2-in-javascript-3ccl</link>
      <guid>https://dev.to/bhumesh_15/whats-arr2-in-javascript-3ccl</guid>
      <description>&lt;p&gt;Ever wondered what is arr['1'] in JavaScript? I always felt that the array index should be a non-negative integer, but i was wrong. Let me explain...&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an Array in JavaScript?
&lt;/h3&gt;

&lt;p&gt;First, &lt;strong&gt;let us understand what exactly an array is.&lt;/strong&gt; Arrays in JavaScript are &lt;strong&gt;special objects&lt;/strong&gt;, but they work differently from regular objects when it comes to property names (or keys).JavaScript arrays use non-negative integers (0, 1, 2, etc.) as valid indices.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3];
console.log(arr); 
// Internally, it behaves like an object: { 0: 1, 1: 2, 2: 3 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, numeric indices are just keys. &lt;br&gt;
We can also check that the array is an object by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(typeof arr) // output: object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How Does JavaScript Handle Array Indices?
&lt;/h3&gt;

&lt;p&gt;Dot notation cannot be used when the property name starts with a number. That is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(arr.1); // a syntax error
console.log(arr[1]); // correct way to access an element
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thus, an array is an object whose key values are non-negative integers and one can access its element using the square brackets and the index.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Happens When You Use arr['1']?
&lt;/h3&gt;

&lt;p&gt;Now, Let's come back to the title of this post, that is what is arr['1'] in JavaScript?&lt;br&gt;
Since array is an object, thus its keys can be string. &lt;em&gt;JavaScript allows accessing array elements using string representations of numbers given that the string that we are using is a representation of a valid number&lt;/em&gt;. &lt;strong&gt;If you try to use a string (other than a string representation of a number), the array treats it as an object property, not an actual array index.&lt;/strong&gt; Let me explain with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1, 2, 3, 4, 5, 6];
console.log(arr[1] === arr['1']); // true
console.log(arr[1] === arr['01']); // false
console.log(arr['1']) // 2
console.log(arr['01']) // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;console.log(arr[1] === arr['1']);&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;here the string '1' will coerce into a number 1, hence it acts like an array index.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Both access the same element (2 in this case), so the comparison returns true&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;console.log(arr[1] === arr['01']);&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;arr[1] still accesses the element at index 1 (which is 2). However, arr['01'] is different - JavaScript doesn't coerce '01' into 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;'01' is treated as a literal string property name (like a regular object key). Since no property named '01' exists on the array, it returns undefined&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Therefore, 2 === undefined is false&lt;/p&gt;
&lt;h3&gt;
  
  
  Key Takeaways
&lt;/h3&gt;

&lt;p&gt;At the end, summarize the main points:&lt;br&gt;
✅ JavaScript arrays are objects with numeric keys.&lt;br&gt;
✅ String representation of numbers (e.g., '2') gets coerced into a number.&lt;br&gt;
✅ Arbitrary strings (e.g., '02') are treated as regular object properties.&lt;br&gt;
Hope you understood this interesting behaviour of JavaScript.Understanding these quirks will help you write better, bug-free JavaScript code. 🚀 Happy coding!&lt;br&gt;
References: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
