<?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>Forem: sromelrey</title>
    <description>The latest articles on Forem by sromelrey (@sromelrey).</description>
    <link>https://forem.com/sromelrey</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%2F410885%2F416cf949-81b8-4468-9750-7bf1d7d8a1c1.jpeg</url>
      <title>Forem: sromelrey</title>
      <link>https://forem.com/sromelrey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sromelrey"/>
    <language>en</language>
    <item>
      <title>JavaScript Concepts: Declaration, Initialization, Mutability, Immutability and Reassignment</title>
      <dc:creator>sromelrey</dc:creator>
      <pubDate>Sun, 26 May 2024 15:09:08 +0000</pubDate>
      <link>https://forem.com/sromelrey/javascript-concepts-declaration-initialization-mutability-immutability-and-reassignment-1j1c</link>
      <guid>https://forem.com/sromelrey/javascript-concepts-declaration-initialization-mutability-immutability-and-reassignment-1j1c</guid>
      <description>&lt;p&gt;This blog is dedicated to understand the following JavaScript Concepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declaration&lt;/li&gt;
&lt;li&gt;Initialization&lt;/li&gt;
&lt;li&gt;Mutability&lt;/li&gt;
&lt;li&gt;Immutability&lt;/li&gt;
&lt;li&gt;Reassignment &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Declaration
&lt;/h3&gt;

&lt;p&gt;This is the phase where you tell the JavaScript engine about the variable. 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 age; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;declares that there is a variable named age.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Initialization
&lt;/h3&gt;

&lt;p&gt;This phase involves assigning a value to the variable. If you do not explicitly initialize the variable at the time of declaration, JavaScript automatically initializes it to &lt;code&gt;undefined&lt;/code&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 a;
console.log(a);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Output: undefined&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Mutability
&lt;/h3&gt;

&lt;p&gt;This refers to the ability to alter the internal state or content of a data structure or object in place. For example, if we have object &lt;code&gt;obj&lt;/code&gt;  and you changed a property within it, you would be mutating the object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = { key: "value" }; 
obj.key = "new value"; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Mutating the object by changing a property&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Immutability
&lt;/h3&gt;

&lt;p&gt;Immutability refers to the inability to alter the state or content of a variable after it has been created. When a variable holds an immutable value, any modification results in the creation of a new value rather than changing the original.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let greeting = "Hello";
let newGreeting = greeting.replace("H", "J");

console.log(greeting);      // Output: "Hello"
console.log(newGreeting);   // Output: "Jello"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;greeting&lt;/code&gt; is a string with the value &lt;code&gt;Hello&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The replace method creates a new string &lt;code&gt;newGreeting&lt;/code&gt; with the value &lt;code&gt;Jello&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The original greeting string remains unchanged.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Immutability ensures that primitive values remain constant, preventing unintended side effects. Any operation that attempts to modify an immutable value creates a new value, leaving the original unchanged.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Reassignment
&lt;/h3&gt;

&lt;p&gt;This refers to the act of assigning a new value to a variable. This action doesn't alter the original value; rather, it updates what the variable points to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let y = "hello"; 
y = "world";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Reassigning y to reference a different string&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this topic, we explored key JavaScript concepts: declaration, initialization, mutability, immutability, and reassignment. Understanding these concepts helps in writing clear and predictable code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declaration introduces a variable.&lt;/li&gt;
&lt;li&gt;Initialization assigns an initial value.&lt;/li&gt;
&lt;li&gt;Mutability allows modification of an object's state.&lt;/li&gt;
&lt;li&gt;Immutability prevents changes to a value, ensuring data integrity.&lt;/li&gt;
&lt;li&gt;Reassignment updates the variable's reference without altering the original value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Grasping these concepts is essential for effective JavaScript programming.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Understanding JavaScript keyword var, let, const and hoisting.</title>
      <dc:creator>sromelrey</dc:creator>
      <pubDate>Sun, 26 May 2024 09:51:50 +0000</pubDate>
      <link>https://forem.com/sromelrey/understanding-javascript-keyword-var-let-const-and-hoisting-jhh</link>
      <guid>https://forem.com/sromelrey/understanding-javascript-keyword-var-let-const-and-hoisting-jhh</guid>
      <description>&lt;p&gt;Good day, everyone! Today, we'll explore how &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt; work and how hoisting affects each.&lt;/p&gt;

&lt;p&gt;Before we dive in, I recommend reading my blog on execution context for a deeper understanding of hoisting.&lt;br&gt;
&lt;a href="https://dev.to/sromelrey/javascript-execution-context-and-js-engine-components-1kem"&gt;JavaScript Execution Context and JS Engine Components&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Goals and Objectives in this topic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Understand how &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt; works under the hood.&lt;/li&gt;
&lt;li&gt;How hoisting affects each keyword.&lt;/li&gt;
&lt;li&gt;What is Temporal Dead Zone ?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The keyword &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt; is what we use when declaring a variable in JavaScript.&lt;/p&gt;

&lt;p&gt;Let's start with the &lt;code&gt;var&lt;/code&gt; keyword before ES6 declaring variable can only be done using &lt;code&gt;var&lt;/code&gt; the following are the features of this keyword:&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;var&lt;/code&gt; keyword:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Function Scope:&lt;/strong&gt; Variables declared with &lt;code&gt;var&lt;/code&gt; are scoped to the function in which they are declared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hoisting:&lt;/strong&gt; Variables are hoisted to the top of their scope and initialized with &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Re-declaration:&lt;/strong&gt; The same variable can be declared multiple times within the same scope without causing an error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Global Object Property:&lt;/strong&gt; If declared outside a function, var becomes a property of the global object (e.g., window in browsers).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The 'let' and 'const' keywords are features introduced in ES6, addressing the shortcomings of the 'var' keyword.&lt;br&gt;
Here's a list of how and what you can use this keywords:&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;let&lt;/code&gt; keyword:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Block Scope:&lt;/strong&gt; Variables declared with &lt;code&gt;let&lt;/code&gt; are scoped to the block in which they are declared (e.g., inside a &lt;code&gt;{}&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hoisting:&lt;/strong&gt; Variables are hoisted but not initialized, leading to a &lt;strong&gt;&lt;em&gt;temporal dead zone&lt;/em&gt;&lt;/strong&gt; until the declaration is encountered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Re-declaration:&lt;/strong&gt; Cannot be re-declared within the same scope, preventing accidental redefinitions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Temporal Dead Zone:&lt;/strong&gt; Accessing the variable before its declaration results in a Reference Error.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;const&lt;/code&gt; keyword:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Block Scope:&lt;/strong&gt; Variables declared with const are scoped to the block in which they are declared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hoisting:&lt;/strong&gt; Variables are hoisted but not initialized, leading to a &lt;strong&gt;&lt;em&gt;temporal dead zone&lt;/em&gt;&lt;/strong&gt; until the declaration is encountered.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Re-declaration:&lt;/strong&gt; Cannot be re-declared within the same scope, similar to let.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Immutability:&lt;/strong&gt; Must be initialized at the time of declaration and cannot be reassigned. However, if the variable holds an object, the object's properties can still be modified.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Temporal Dead Zone:&lt;/strong&gt; Accessing the variable before its declaration results in a Reference Error.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Understanding how hoisting works with different keywords
&lt;/h3&gt;

&lt;p&gt;What is hoisting? Hoisting is a feature in JavaScript that allows you to use variables or invoke functions before they are declared. Here's a sample code to illustrate this concept:&lt;/p&gt;
&lt;h5&gt;
  
  
  Here's a sample code of hoisting using &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;function declaration&lt;/code&gt;
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// * Accessing the 'age' variable before it is initialized&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;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// ? Will log undefined&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// * Invoking the 'logAge' function before it is declared&lt;/span&gt;
&lt;span class="nf"&gt;logAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// ? Will log AGE is 12&lt;/span&gt;

&lt;span class="c1"&gt;// * Invoking logAge Function Declaration before it's being declare&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;logAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ageArg&lt;/span&gt;&lt;span class="p"&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="s2"&gt;`log AGE is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;ageArg&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;h5&gt;
  
  
  Here's a sample code of hoisting using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;function expression&lt;/code&gt;
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// * Accessing let age before it's initialization&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;age&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// * Invoking logAge Function Expression before it's being declare&lt;/span&gt;
&lt;span class="nf"&gt;logAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&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;logAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&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="s2"&gt;`log AGE is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;ageArg&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;blockquote&gt;
&lt;p&gt;I will write a separate blog post under the topic of functions to further explain how hoisting works with function expressions and function declarations when invoking a function.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Temporal Dead Zone
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;&lt;em&gt;temporal dead zone&lt;/em&gt;&lt;/strong&gt; starts from the block until the &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; variable declaration is processed. In other words, it is the location where you cannot access the let variables before they are defined.&lt;/p&gt;

&lt;p&gt;Here's a sample code to demonstrate the &lt;strong&gt;&lt;em&gt;TDZ&lt;/em&gt;&lt;/strong&gt;&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;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;myVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: Cannot access 'myVar' before initialization&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myVar&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;myVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Although 'let' and 'const' are hoisted, accessing them before their declaration results in a Reference Error, unlike 'var' which returns undefined. This behavior is known as the 'temporal dead zone."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Understanding how var, let, and const work and how hoisting affects each keyword is crucial for writing efficient JavaScript code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; is function-scoped, hoisted, and can be re-declared within the same scope, which can lead to unexpected behaviors.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are block-scoped, also hoisted but not initialized until their declaration, leading to the temporal dead zone which prevents access before initialization.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;const&lt;/code&gt; also enforces immutability, meaning it must be initialized during declaration and cannot be reassigned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hoisting&lt;/em&gt;&lt;/strong&gt; allows functions and variables to be used before they are declared, but understanding the nuances between these keywords helps prevent common pitfalls, such as reference errors or unintended variable reassignments. For more details on hoisting with function expressions and declarations, stay tuned for my upcoming blog post. &lt;/p&gt;

&lt;p&gt;Thanks for reading 😁😁😁😁&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Concept : Memory Allocation(Stack and Heap) and Behavior</title>
      <dc:creator>sromelrey</dc:creator>
      <pubDate>Sun, 26 May 2024 03:54:57 +0000</pubDate>
      <link>https://forem.com/sromelrey/javascript-concept-memory-allocation-and-behavior-485b</link>
      <guid>https://forem.com/sromelrey/javascript-concept-memory-allocation-and-behavior-485b</guid>
      <description>&lt;p&gt;Good day everyone! Today we will discuss a JavaScript Concept Memory Allocation and Behavior. This concept is crucial to understand deeply on how JavaScript works. Learn alongside me and Enjoy reading!&lt;/p&gt;

&lt;h3&gt;
  
  
  Goals and Objectives for this Topic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Understand how &lt;strong&gt;&lt;em&gt;memory allocation&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;behavior&lt;/em&gt;&lt;/strong&gt; function in:

&lt;ul&gt;
&lt;li&gt;Primitive types&lt;/li&gt;
&lt;li&gt;Reference types&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In JavaScript engines, there are indeed two primary memory spaces used for data allocation: Stack and Heap&lt;/p&gt;

&lt;h4&gt;
  
  
  Primitive Types
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Storage:&lt;/strong&gt; Primitive types are stored in &lt;strong&gt;&lt;em&gt;Stack Memory&lt;/em&gt;&lt;/strong&gt; . The stack is generally used for static memory allocation, which includes fixed-size variables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Value-based:&lt;/strong&gt; When you assign or pass a &lt;code&gt;primitive type&lt;/code&gt;, it is &lt;em&gt;done by value&lt;/em&gt;. This means that the actual value is copied.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Example:
&lt;/h5&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;a&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Copies the value of a into a new memory spot for b&lt;/span&gt;

&lt;span class="nx"&gt;b&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="c1"&gt;// Changes in b do not affect a&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 10&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;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Reference types
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Storage:&lt;/strong&gt; Reference types are stored in &lt;strong&gt;&lt;em&gt;Heap Memory&lt;/em&gt;&lt;/strong&gt;. The heap is used for dynamic memory allocation, where the size of the structure can change over time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reference-based:&lt;/strong&gt; When you assign or pass reference types, it is &lt;em&gt;done by reference&lt;/em&gt;. This means that instead of copying the actual data, a reference (or a pointer) to the object in memory is copied.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&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;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&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;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Copies the reference, not the actual object&lt;/span&gt;

&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Changes the value via obj2 also affect obj1 &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;obj1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 15 &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;obj2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How JavaScript Manages Memory
&lt;/h2&gt;

&lt;p&gt;JavaScript automatically manages memory with a garbage collector, freeing developers from manually deallocating memory. The garbage collector periodically frees memory used by data that is no longer accessible.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Stack
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The stack is a Last-In-First-Out (LIFO) data structure used for storing:

&lt;ul&gt;
&lt;li&gt;Local variables declared within the function.&lt;/li&gt;
&lt;li&gt;Arguments passed to the function.&lt;/li&gt;
&lt;li&gt;The return address (where to return after the function finishes execution).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;It's fast and has a fixed size, pre-allocated by the OS. Once a function completes, its data is automatically removed, freeing space for new calls.
&lt;h4&gt;
  
  
  2. Heap&lt;/h4&gt;
&lt;/li&gt;



&lt;li&gt;The heap is an unstructured, flexible memory area for dynamic allocation. It's slower than the stack but stores:

&lt;ul&gt;
&lt;li&gt;Global variables&lt;/li&gt;
&lt;li&gt;Objects and arrays&lt;/li&gt;
&lt;li&gt;Dynamically allocated data&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Garbage collection automatically reclaims unused memory, preventing leaks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Summary of the key difference:
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp5hjpde8o4059bbgll2j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp5hjpde8o4059bbgll2j.png" alt="Image description" width="800" height="411"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Understanding the roles of the stack and heap is crucial for writing efficient JavaScript code. You should strive to minimize the use of global variables and large data structures within functions to optimize memory usage and avoid stack overflows. Thanks for reading ❤️❤️❤️!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>deeplearning</category>
    </item>
    <item>
      <title>JavaScript Data Types Primitive vs Non-Primitive</title>
      <dc:creator>sromelrey</dc:creator>
      <pubDate>Sat, 25 May 2024 07:05:41 +0000</pubDate>
      <link>https://forem.com/sromelrey/javascript-data-types-primitive-vs-non-primitive-1920</link>
      <guid>https://forem.com/sromelrey/javascript-data-types-primitive-vs-non-primitive-1920</guid>
      <description>&lt;p&gt;Good day Everyone! Today we will discuss the Data Types of JavaScript Primitive and Non-Primitive. Learn alongside me, and share your own insights. Enjoy the read!&lt;/p&gt;

&lt;h2&gt;
  
  
  Goals and Objectives in this topic:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Understand the concept of Data Types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Grasp the fundamental idea of data types in JavaScript.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Differentiate Between Primitive and Non-Primitive Data Types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clearly distinguish between primitive and non-primitive types.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Understand the immutability of primitive and mutability of non-primitive types&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  In JavaScript, data types define the kind of data a &lt;em&gt;variable can hold and how that data is stored and manipulated&lt;/em&gt;.
&lt;/h4&gt;

&lt;p&gt;There are two main categories of data types:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Primitive Data Types:
&lt;/h3&gt;

&lt;p&gt;Primitive data types represent simple, fundamental values. They are immutable, meaning their values cannot be changed directly after they are assigned. When you reassign a new value to a primitive variable, a new memory location is created to store the new value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here's a simple example to illustrate the immutability of primitive types:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 12;

age = 2; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  1. Memory Allocation and Assignment:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Initially, age is declared with var. It gets assigned undefined (default for var).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, age is assigned the value 12. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Memory is allocated in the Heap to store this value.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Reassignment:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When you write age = 2;, a new memory location is allocated in the Heap to store the value 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The variable age now references this new memory location.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Garbage Collection:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript has a garbage collector that automatically cleans up unused memory.&lt;/li&gt;
&lt;li&gt;Since there are no more references to the memory location that held 12 (the original value), it becomes a candidate for garbage collection.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Summary:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The value &lt;code&gt;12&lt;/code&gt; is no longer accessible through the variable age after the reassignment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It might eventually be removed from the Heap by the garbage collector when it determines it's no longer needed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are the common &lt;code&gt;primitive&lt;/code&gt;data types in JavaScript:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Number:&lt;/strong&gt; Represents numeric values, including integers (whole numbers) and decimals. (e.g., 12, 3.14, -100)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;String:&lt;/strong&gt; Represents sequences of characters, used for text. (e.g., "Hello", 'World')&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Boolean:&lt;/strong&gt; Represents logical values, either true or false.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Undefined:&lt;/strong&gt; Indicates that a variable has been declared but not yet assigned a value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Null:&lt;/strong&gt; Represents the intentional absence of a value. (different from undefined)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Symbol (ES6+):&lt;/strong&gt; A unique and immutable identifier (rarely used).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;BigInt (ES 2020):&lt;/strong&gt; A type which is the built-in object that can represent whole numbers larger than &lt;code&gt;253 – 1&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  2. Non-Primitive Data Types:
&lt;/h3&gt;

&lt;p&gt;Non-primitive data types, also called reference types, are more complex and store collections of data. They are mutable, meaning their content can be modified after they are created. When you assign a non-primitive data type to a variable, you're storing a reference (memory address) to the actual data location in the Heap (unstructured storage area). Any changes made through the variable reference will affect the original data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here's a simple example to illustrate the mutability of Non-primitive
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// * Object Creation&lt;/span&gt;
&lt;span class="c1"&gt;// ? We create an object person with properties name ("Alice") and age (30):&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// * Initial Output:&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;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// ? Output: { name: "Alice", age: 30 }&lt;/span&gt;

&lt;span class="c1"&gt;// * Object Mutation:&lt;/span&gt;
&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// ? Modifying a property of the object&lt;/span&gt;

&lt;span class="c1"&gt;// * Final Output:&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;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;//? Output: { name: "Alice", age: 31 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Points:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;const&lt;/code&gt; prevents reassignment of the object but allows property modification.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;person.age = 31; changes the age property, showing object mutability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can modify or add properties using dot notation (e.g., person.city = "New York";).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some common non-primitive data types in JavaScript:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Object:&lt;/strong&gt; A collection of key-value pairs used to store structured data. &lt;br&gt;
(e.g., &lt;code&gt;{ name: "Alice", age: 30 }&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Array:&lt;/strong&gt; An ordered collection of items, which can hold different data types. &lt;br&gt;
(e.g., &lt;code&gt;[1, "apple", true]&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Function:&lt;/strong&gt; A reusable block of code that performs a specific task.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Key Differences:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1lv07gyihfvan4u7usc4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1lv07gyihfvan4u7usc4.png" alt="Image description" width="800" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Understanding primitive and non-primitive data types is crucial for writing efficient and predictable JavaScript code. You need to choose the appropriate data type based on the kind of data you want to store and manipulate.&lt;/p&gt;

&lt;p&gt;This understanding enables you to write more efficient and predictable JavaScript code.&lt;/p&gt;

&lt;p&gt;Thanks for reading ❤️❤️❤️! &lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Execution Context and JS Engine Components</title>
      <dc:creator>sromelrey</dc:creator>
      <pubDate>Fri, 24 May 2024 13:03:36 +0000</pubDate>
      <link>https://forem.com/sromelrey/javascript-execution-context-and-js-engine-components-1kem</link>
      <guid>https://forem.com/sromelrey/javascript-execution-context-and-js-engine-components-1kem</guid>
      <description>&lt;p&gt;Hello everyone! Welcome to my blog, where I’ll be documenting my journey to mastering the JavaScript language. This is the first post in what will be a series chronicling my experiences, challenges, and triumphs as I dive deep into the world of JavaScript. I hope you'll join me on this adventure, learn alongside me, and share your own insights. Enjoy the read!&lt;/p&gt;

&lt;h2&gt;
  
  
  Goals and Objectives in this topic:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Understand the concept of Execution Context (EC)and it’s phases&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Creation Phase &lt;/li&gt;
&lt;li&gt;Execution Phase&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Understanding how Function behave’s in (EC)&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Creation&lt;/li&gt;
&lt;li&gt;Definition&lt;/li&gt;
&lt;li&gt;Execution&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Understand the following Engine Components&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Clean Up&lt;/li&gt;
&lt;li&gt;Call Stack&lt;/li&gt;
&lt;li&gt;Heap&lt;/li&gt;
&lt;li&gt;Built-in Functions and Objects&lt;/li&gt;
&lt;li&gt;Type Information&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understanding the concept of Execution Context (EC) and it’s Phase
&lt;/h2&gt;

&lt;h4&gt;
  
  
  To help us visualize how the EC works and its phases here’s a simple code for demonstration.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 12;

console.log(age);

function logAge(ageArg) {
  console.log(`log AGE is ${ageArg}`);
}

logAge(age);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When the JavaScript Engine Starts Running the Code, it creates a global Execution Context. This context is divided in to two parts Creation Phase and Execution Phase.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Global Execution Context&lt;/strong&gt; (&lt;strong&gt;&lt;em&gt;Creation Phase&lt;/em&gt;&lt;/strong&gt;):
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Memory Allocation&lt;/strong&gt;: Space is reserved in the Heap (&lt;em&gt;unstructured storage&lt;/em&gt;) for the variable &lt;code&gt;age&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initialization&lt;/strong&gt;: The variable &lt;code&gt;age&lt;/code&gt;is initialized with the value &lt;code&gt;undefined&lt;/code&gt;(default for var variables in JavaScript).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In JavaScript with &lt;code&gt;var&lt;/code&gt; variables, initialization with the actual value happens during the &lt;strong&gt;&lt;em&gt;execution phase&lt;/em&gt;&lt;/strong&gt; when the line with the assignment is encountered. The &lt;strong&gt;&lt;em&gt;creation phase&lt;/em&gt;&lt;/strong&gt; only allocates memory and assigns &lt;code&gt;undefined&lt;/code&gt;by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Reference Creation&lt;/strong&gt;: A reference is created in the global scope pointing to the memory location of age in the Heap. This allows you to access age using its name throughout the global scope.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Definition&lt;/strong&gt; (&lt;strong&gt;&lt;em&gt;not Creation&lt;/em&gt;&lt;/strong&gt;): The function &lt;code&gt;logAge&lt;/code&gt;is defined and stored in memory. This includes its code and argument (ageArg). However, the function itself isn't executed yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. Global Execution Context&lt;/strong&gt; (&lt;strong&gt;&lt;em&gt;Execution Phase&lt;/em&gt;&lt;/strong&gt;):
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Variable Assignment&lt;/strong&gt;: The line &lt;code&gt;age = 12;&lt;/code&gt; is encountered. The value 12 is assigned to the previously allocated memory location for age.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console Output&lt;/strong&gt;: The line &lt;code&gt;console.log(age);&lt;/code&gt; is executed. The value of age (which is now 12) is retrieved from the Heap using the reference in the global scope and printed to the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Call&lt;/strong&gt;: The line &lt;code&gt;logAge(age);&lt;/code&gt; is reached. This triggers the creation of a separate execution context for the &lt;code&gt;logAge&lt;/code&gt;function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Each function call creates a new execution context&lt;/strong&gt; to manage the function's execution environment, including its scope, arguments, and call stack frame.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3.Function Execution Context&lt;/strong&gt; for &lt;code&gt;logAge&lt;/code&gt; (&lt;strong&gt;&lt;em&gt;Creation Phase&lt;/em&gt;&lt;/strong&gt;):
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Note: This phase happens only when &lt;code&gt;logAge&lt;/code&gt; is called.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory Allocation&lt;/strong&gt;: If &lt;code&gt;logAge&lt;/code&gt;has any arguments (like ageArg in this case), memory is allocated for them in the Heap within this context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Local Variable Initialization&lt;/strong&gt; (if any): Any variables declared within the &lt;code&gt;logAge&lt;/code&gt;function are initialized, usually with undefined.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4. Function Execution Context&lt;/strong&gt; for &lt;code&gt;logAge&lt;/code&gt;(&lt;strong&gt;&lt;em&gt;Execution Phase&lt;/em&gt;&lt;/strong&gt;):
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Argument Passing:&lt;/strong&gt; The value of age (which is 12 from the global context) is passed as an argument to the &lt;code&gt;logAge&lt;/code&gt;function and assigned to the &lt;code&gt;ageArg&lt;/code&gt;parameter within the function's execution context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function Body Execution:&lt;/strong&gt; The code within the &lt;code&gt;logAge function&lt;/code&gt;is executed. In this case, it uses template literals to construct a string and then logs it using console.log.&lt;/p&gt;

&lt;h4&gt;
  
  
  Overall Sequence:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Global Execution Context (Creation Phase)&lt;/li&gt;
&lt;li&gt;Global Execution Context (Execution Phase) - including variable assignment, console output, and function call&lt;/li&gt;
&lt;li&gt;Function Execution Context for logAge (Creation Phase) - only when the function is called&lt;/li&gt;
&lt;li&gt;Function Execution Context for logAge (Execution Phase) - function body execution&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Cleaning Up&lt;/strong&gt;: The JavaScript engine employs a garbage collector to manage memory. Once the function execution context for logAge is complete and there are no more references to the function arguments or local variables, the memory used by them in the Heap becomes available for garbage collection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Call Stack:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The call stack keeps track of the currently executing function and its arguments.&lt;/li&gt;
&lt;li&gt;In this code, the call stack would have:

&lt;ul&gt;
&lt;li&gt;logAge function call when it's executed.&lt;/li&gt;
&lt;li&gt;The global execution context when the script starts running.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Heap:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Heap stores the values of variables and function arguments during execution.&lt;/li&gt;
&lt;li&gt;In this code, the Heap would store:

&lt;ul&gt;
&lt;li&gt;The value 12 for the variable age.&lt;/li&gt;
&lt;li&gt;The value 12 passed as an argument to the logAge function (stored in the ageArg memory location).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Built-in Functions and Objects:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code uses built-in functions like console.log. These functions are predefined and readily available for use.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Type Information:&lt;/strong&gt;&lt;br&gt;
JavaScript is loosely typed, so the engine doesn't explicitly store type information for variables. In this case, both age and ageArg would hold the numeric value 12.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By understanding the JavaScript Execution Context and its components, you've gained valuable insight into how your code is processed and executed.  We've explored the creation and execution phases of the global execution context, as well as the creation and execution phases that occur within function calls. This knowledge helps you write more efficient and predictable JavaScript code.&lt;/p&gt;

&lt;p&gt;Thanks for reading 😁😁😁😁&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>deeplearning</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
