<?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: Amardeep</title>
    <description>The latest articles on DEV Community by Amardeep (@amar_x_vr).</description>
    <link>https://dev.to/amar_x_vr</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%2F2205960%2Fe2edbbbb-ba2a-43ec-b733-10d5c7f0c245.jpg</url>
      <title>DEV Community: Amardeep</title>
      <link>https://dev.to/amar_x_vr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amar_x_vr"/>
    <language>en</language>
    <item>
      <title>Understanding Stack and Heap in JavaScript .</title>
      <dc:creator>Amardeep</dc:creator>
      <pubDate>Sun, 13 Oct 2024 15:18:03 +0000</pubDate>
      <link>https://dev.to/amar_x_vr/understanding-stack-and-heap-in-javascript--11ho</link>
      <guid>https://dev.to/amar_x_vr/understanding-stack-and-heap-in-javascript--11ho</guid>
      <description>&lt;p&gt;In JavaScript, stack and heap are two types of memory used for managing data, each with a distinct purpose: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stack &lt;/li&gt;
&lt;li&gt;Heap &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;*&lt;em&gt;What are Stack and Heap *&lt;/em&gt;&lt;br&gt;
Stack : The Stack is used for static memory allocation, primarily for storing primitive types and function calls. It's a simple, last-in, first-out (LIFO) structure, making it very fast to access.&lt;/p&gt;

&lt;p&gt;Heap : The Heap is used for dynamic memory allocation, where objects and arrays (non-primitive types) are stored. Unlike the Stack, the Heap is more complex and slower to access, as it allows for flexible memory allocation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of Stack Memory :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myName = "Amardeep"; //primitive type stored in stack 
let nickname = myName; // A copy of the value is created in the Stack 
nickname = "Rishu"; // Now changing the copy does not affect the original value .
console.log(myName); // output =&amp;gt; Amardeep (Original values remains unchanged since we are using stack)
console.log(nickname); // output =&amp;gt; rishu (only the copied value will changed)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;myName is stored in the Stack as a primitive type.&lt;/li&gt;
&lt;li&gt;When nickname is assigned the value of myName , a copy of that value is created in the Stack .&lt;/li&gt;
&lt;li&gt;Changing nickname doesn't affect myName , since they are independent copies in the memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example of Heap Memory&lt;/strong&gt;&lt;br&gt;
Now lets check how non-primitive data types(objects) are managed in the Heap .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let userOne = {         // The reference to this object is stored in the Stack.
    email: "user@google.com",
    upi: "user@ybl"
};                      // The actual object data is stored in the Heap.

let userTwo = userOne;  // userTwo references the same object in the Heap.

userTwo.email = "amar@google.com"; // Modifying userTwo also affects userOne.

console.log(userOne.email); // Output: amar@google.com
console.log(userTwo.email); // Output: amar@google.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;userOne holds a reference to an object stored in the Heap.
-userTwo is assigned the same reference, meaning both userOne and userTwo point to the same object in the Heap.
-Changing userTwo.email directly affects userOne.email, because both references point to the same location in memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Takeaways&lt;/strong&gt;&lt;br&gt;
*&lt;em&gt;Stack Memory *&lt;/em&gt; is used for storing primitive types and function calls . Each time you assign a value , a new copy is created in the Stack.&lt;br&gt;
*&lt;em&gt;Heap Memory *&lt;/em&gt; is used for storing objects and arrays . Variables that reference the same object share the same memory location in memory , so changing one variable affects the others .&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
