<?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: Deepanshu Gupta</title>
    <description>The latest articles on DEV Community by Deepanshu Gupta (@deepanshu).</description>
    <link>https://dev.to/deepanshu</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%2F1051075%2Fad62af88-cdc9-417c-bd46-d72e9271703e.png</url>
      <title>DEV Community: Deepanshu Gupta</title>
      <link>https://dev.to/deepanshu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deepanshu"/>
    <language>en</language>
    <item>
      <title>.sort method in JavaScript may not sort numbers the way you would expect.</title>
      <dc:creator>Deepanshu Gupta</dc:creator>
      <pubDate>Fri, 24 Mar 2023 09:31:55 +0000</pubDate>
      <link>https://dev.to/deepanshu/sort-method-in-javascript-1khg</link>
      <guid>https://dev.to/deepanshu/sort-method-in-javascript-1khg</guid>
      <description>&lt;p&gt;In JavaScript, the array.sort() method sorts the array.&lt;br&gt;
Now, the data type in the array could be anything, either numbers or strings.&lt;br&gt;
When we talk about sorting through an array that contains a string, .sort() method seems to work just fine. There is a caveat though on how this sorting actually works.&lt;/p&gt;
&lt;h2&gt;
  
  
  Behind the scenes of sorting
&lt;/h2&gt;

&lt;p&gt;.sort() works in a peculiar way, i.e. it converts the initial letter(s) of the string into equivalent ASCII characters and then, the converted values are compared, and therefore sorted.&lt;br&gt;
By value, capital letters have ASCII values with low number equivalent, therefore considered small, and they appear first then the small case letters since the default method is to sort in ascending order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const names = ['joker', 'batman', 'catwoman'];
names.sort(); // =&amp;gt; ['batman', 'catwoman', 'joker']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  In the case of numbers, sorting works not as you would expect
&lt;/h2&gt;

&lt;p&gt;Same way, the numbers are converted into their equivalent ASCII characters values and then the new equivalent values are compared, similar to how a dictionary algorithm works, which therefore fetches us wrong results. Therefore to expect the right behaviour from sort method in case of sorting of numbers, we pass a callback function in the .sort() as parameters, which return either a-b ( for ascending order of sorting ) or return b-a for descending order of sorting. &lt;br&gt;
Please refer to the examples below to have a better understanding of how this works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [10, 5, 11];
numbers.sort(); // =&amp;gt; [10, 11, 5]

const numbers = [10, 5, 11];
numbers.sort((a, b) =&amp;gt; a - b); // =&amp;gt; [5, 10, 11]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Understanding the difference: Primitive Vs Reference Data types in JavaScript.</title>
      <dc:creator>Deepanshu Gupta</dc:creator>
      <pubDate>Thu, 23 Mar 2023 17:30:00 +0000</pubDate>
      <link>https://dev.to/deepanshu/understanding-the-difference-primitive-vs-reference-data-types-in-javascript-4pg7</link>
      <guid>https://dev.to/deepanshu/understanding-the-difference-primitive-vs-reference-data-types-in-javascript-4pg7</guid>
      <description>&lt;p&gt;JavaScript has majorly two kind of data type categories,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Primitive Data type&lt;/li&gt;
&lt;li&gt;Reference Data type&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And the major differentiation between the two happens on the grounds of how the data types are stored in the memory. Below we will discuss about how this all works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Primitive data types
&lt;/h2&gt;

&lt;p&gt;These data types are stored in a much simple way than reference, below are the examples :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Numbers &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Strings&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Boolean&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Null&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Undefined&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Symbols&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How are they stored in the memory?
&lt;/h2&gt;

&lt;p&gt;When a primitive data type is declared, it gets stored on the stack, and it is identified by the variable name used to declare it. &lt;/p&gt;

&lt;p&gt;Let's consider 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 num1 = 29;
let num2 = num1;
//num1=num2=29;
//now we may reassign a new value to num1
let num1 = 45;
console.log(num1,num2);
//gives num1 = 45, but num2 = 29

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This happens because each time we declare a primitive data type, this gets stored in a stack, i.e. the computer creates room for num1 and when we assign the same value to num2, it gets a separate memory location. Therefore, any changes to num1 do not reflect upon num2, since they both are stored at different places. &lt;/p&gt;

&lt;h2&gt;
  
  
  Reference data types
&lt;/h2&gt;

&lt;p&gt;These types of data types are particularly objects. Examples are :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Objects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Functions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Collections&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Arrays&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dates&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How are they stored in the memory?
&lt;/h2&gt;

&lt;p&gt;They are stored a bit differently in the memory, let's see how.&lt;/p&gt;

&lt;p&gt;Whenever a reference data type is created, the variable name with which it is declared gets a pointer value assigned to it instead of the actual data value, this pointer value is an address to the memory location where the reference data type value is stored, in heap, a totally different memory location than the stack.&lt;br&gt;
For 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 arr1 = [1,2,4];
let arr2 = arr1;
//now we may reassign value to arr1 = [1,2,34];
let arr1 = [1,2,34];
console.log(arr1,arr2);
//gives an output where now arr1 = arr2 = [1,2,34];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This happens because when arr1 is created, the value gets stored in the heap, and arr1 gets a pointer as the value assigned, in the stack, and when arr2= arr1, arr2 gets a pointer with the same address value since the array was created only once, unlike in the case of primitive data type. Therefore, when a change is made to the array value, the change reflects in both arrays, since they point to the same value. &lt;/p&gt;

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