<?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: HARSHITH GADDAM</title>
    <description>The latest articles on DEV Community by HARSHITH GADDAM (@harshith_gaddam).</description>
    <link>https://dev.to/harshith_gaddam</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%2F4039220%2F9859a55c-0f5a-4fe1-9c74-96e155044b54.jpg</url>
      <title>DEV Community: HARSHITH GADDAM</title>
      <link>https://dev.to/harshith_gaddam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harshith_gaddam"/>
    <language>en</language>
    <item>
      <title>My JavaScript Learning Journey:Understanding the JavaScript Event Loop and Concurrency Model</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Tue, 21 Jul 2026 13:31:11 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/my-javascript-learning-journeyunderstanding-the-javascript-event-loop-and-concurrency-model-4oec</link>
      <guid>https://dev.to/harshith_gaddam/my-javascript-learning-journeyunderstanding-the-javascript-event-loop-and-concurrency-model-4oec</guid>
      <description>&lt;p&gt;Today, I learned one of the most important concepts in JavaScript: the Event Loop and the Concurrency Model.&lt;/p&gt;

&lt;h1&gt;
  
  
  JavaScript is Single-Threaded
&lt;/h1&gt;

&lt;p&gt;JavaScript is a single-threaded language. This means it can execute only one piece of JavaScript code at a time.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;console.log("A");&lt;br&gt;
console.log("B");&lt;br&gt;
console.log("C");&lt;/p&gt;

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

&lt;p&gt;A&lt;br&gt;
B&lt;br&gt;
C&lt;/p&gt;

&lt;p&gt;JavaScript executes these statements one after another.&lt;/p&gt;




&lt;h1&gt;
  
  
  What is the Call Stack?
&lt;/h1&gt;

&lt;p&gt;The Call Stack is where JavaScript executes functions. Whenever a function is called, it is added to the stack. After the function finishes executing, it is removed from the stack.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;function one() {&lt;br&gt;
    two();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;function two() {&lt;br&gt;
    console.log("Hello");&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;one();&lt;/p&gt;

&lt;p&gt;Execution order:&lt;/p&gt;

&lt;p&gt;one()&lt;br&gt;
↓&lt;br&gt;
two()&lt;br&gt;
↓&lt;br&gt;
console.log()&lt;br&gt;
↓&lt;br&gt;
Stack becomes empty&lt;/p&gt;

&lt;p&gt;The Call Stack follows the Last In, First Out (LIFO) principle.&lt;/p&gt;

&lt;h1&gt;
  
  
  What are Web APIs and Node APIs?
&lt;/h1&gt;

&lt;p&gt;Some operations take time, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Waiting for a timer&lt;/li&gt;
&lt;li&gt;Making an API request&lt;/li&gt;
&lt;li&gt;Reading a file&lt;/li&gt;
&lt;li&gt;Listening for button clicks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript itself cannot perform these operations.&lt;/p&gt;

&lt;p&gt;Instead, it asks the runtime to handle them.&lt;/p&gt;

&lt;h3&gt;
  
  
  In Browsers
&lt;/h3&gt;

&lt;p&gt;The browser provides Web APIs such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;setTimeout()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setInterval()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fetch()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;addEventListener()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;DOM APIs&lt;/li&gt;
&lt;li&gt;&lt;code&gt;localStorage&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  In Node.js
&lt;/h3&gt;

&lt;p&gt;Node.js provides Node APIs such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.readFile()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fs.writeFile()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;path&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;os&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These APIs perform asynchronous work outside the JavaScript engine.&lt;/p&gt;




&lt;h1&gt;
  
  
  Callback (Macrotask) Queue
&lt;/h1&gt;

&lt;p&gt;When an asynchronous operation like &lt;code&gt;setTimeout()&lt;/code&gt; finishes, its callback does not execute immediately.&lt;/p&gt;

&lt;p&gt;Instead, it is placed into the Callback Queue, also called the Macrotask Queue.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;setTimeout(() =&amp;gt; {&lt;br&gt;
    console.log("Timer");&lt;br&gt;
}, 1000);&lt;/p&gt;

&lt;p&gt;After one second Call back function added to Callback Queue.&lt;br&gt;
It waits there until the Event Loop moves it to the Call Stack.&lt;/p&gt;

&lt;h1&gt;
  
  
  Microtask Queue
&lt;/h1&gt;

&lt;p&gt;The Microtask Queue has a higher priority than the Callback Queue.&lt;/p&gt;

&lt;p&gt;The following callbacks are added to the Microtask Queue:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Promise.then()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.catch()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.finally()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;await&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;queueMicrotask()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Promise.resolve().then(() =&amp;gt; {&lt;br&gt;
    console.log("Promise");&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;The callback is placed in the Microtask Queue.&lt;/p&gt;

&lt;h1&gt;
  
  
  Event Loop
&lt;/h1&gt;

&lt;p&gt;The Event Loop is responsible for checking whether the Call Stack is empty.&lt;/p&gt;

&lt;p&gt;When the Call Stack becomes empty, it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Executes all Microtasks&lt;/li&gt;
&lt;li&gt;Executes one Callback (Macrotask)&lt;/li&gt;
&lt;li&gt;Again checks call stack is empty or not and does 1&amp;amp;2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It does not execute code itself. It simply moves callbacks from the queues to the Call Stack.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why do Promises execute before setTimeout()?
&lt;/h1&gt;

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

&lt;p&gt;console.log("Start");&lt;/p&gt;

&lt;p&gt;setTimeout(() =&amp;gt; {&lt;br&gt;
    console.log("Timeout");&lt;br&gt;
}, 0);&lt;/p&gt;

&lt;p&gt;Promise.resolve().then(() =&amp;gt; {&lt;br&gt;
    console.log("Promise");&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;console.log("End");&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
Start&lt;br&gt;
End&lt;br&gt;
Promise&lt;br&gt;
Timeout&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Promise.then()&lt;/code&gt; goes to the &lt;strong&gt;Microtask Queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout()&lt;/code&gt; goes to the &lt;strong&gt;Callback (Macrotask) Queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The Event Loop always executes &lt;strong&gt;all Microtasks first&lt;/strong&gt; before executing Macrotasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Does setTimeout() execute exactly on time?
&lt;/h1&gt;

&lt;p&gt;A common misunderstanding is that:&lt;/p&gt;

&lt;p&gt;setTimeout(callback, 1000);&lt;/p&gt;

&lt;p&gt;means the callback will execute exactly after one second.&lt;/p&gt;

&lt;p&gt;This is not true.&lt;/p&gt;

&lt;p&gt;It means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Execute the callback after at least one second.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the Call Stack is busy with synchronous code, the callback has to wait.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;setTimeout(() =&amp;gt; {&lt;br&gt;
    console.log("Done");&lt;br&gt;
}, 1000);&lt;/p&gt;

&lt;p&gt;// Imagine this loop takes 3 seconds&lt;br&gt;
for (let i = 0; i &amp;lt; 1e10; i++) {}&lt;/p&gt;

&lt;p&gt;console.log("Finished");&lt;/p&gt;

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

&lt;p&gt;Even though the timer finished after one second, the callback waited until the Call Stack became empty.&lt;/p&gt;




&lt;h1&gt;
  
  
  What is the JavaScript Concurrency Model?
&lt;/h1&gt;

&lt;p&gt;JavaScript can execute only one line of code at a time, but it can still handle many asynchronous operations.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A timer can be counting.&lt;/li&gt;
&lt;li&gt;A network request can be downloading.&lt;/li&gt;
&lt;li&gt;The browser can listen for button clicks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All these operations are handled by the browser or Node.js while JavaScript continues executing other code.&lt;/p&gt;

&lt;p&gt;When these operations finish, their callbacks are added to the appropriate queue.&lt;/p&gt;

&lt;p&gt;This ability to make multiple operations progress during the same period of time is called the Concurrency Model.&lt;/p&gt;

&lt;h1&gt;
  
  
  Cooperative Concurrency vs True Parallelism
&lt;/h1&gt;

&lt;p&gt;These two terms are often confused.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cooperative Concurrency
&lt;/h3&gt;

&lt;p&gt;Only one JavaScript task runs at a time.&lt;/p&gt;

&lt;p&gt;Tasks take turns executing.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;One chef starts cooking rice, then makes tea while the rice cooks.&lt;/p&gt;

&lt;p&gt;The chef is not doing both tasks at the exact same moment, but both tasks are progressing.&lt;/p&gt;

&lt;p&gt;JavaScript works like this.&lt;/p&gt;

&lt;h3&gt;
  
  
  True Parallelism
&lt;/h3&gt;

&lt;p&gt;Multiple tasks execute at the exact same time using different CPU cores or threads.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chef 1 cooks rice.&lt;/li&gt;
&lt;li&gt;Chef 2 makes tea.&lt;/li&gt;
&lt;li&gt;Chef 3 cuts vegetables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three are working simultaneously.&lt;/p&gt;

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

&lt;p&gt;Learning the Event Loop and the Concurrency Model helped me understand how JavaScript performs asynchronous operations without blocking the main thread. These concepts explain why Promise callbacks execute before &lt;code&gt;setTimeout()&lt;/code&gt;, why timers are not always exact, and how browsers and Node.js work together with JavaScript to build responsive applications.&lt;/p&gt;

&lt;p&gt;Understanding these fundamentals is essential for writing efficient JavaScript code.&lt;br&gt;
Thank you for reading! If you're also learning JavaScript, I hope this summary helps you understand these concepts more clearly. Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>blog</category>
      <category>learning</category>
    </item>
    <item>
      <title>My JavaScript Learning Journey: Understanding How JavaScript Really Works</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Tue, 21 Jul 2026 11:32:29 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/my-javascript-learning-journey-understanding-how-javascript-really-works-37m8</link>
      <guid>https://dev.to/harshith_gaddam/my-javascript-learning-journey-understanding-how-javascript-really-works-37m8</guid>
      <description>&lt;p&gt;During this learning session, I focused on JavaScript's internal working, including Execution Context, Variables, Scope, Closures, Hoisting, this binding, Call Stack, and Clean Code Principles like DRY and KISS.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;JavaScript Execution Context&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first topic I learned was Execution Context.&lt;/p&gt;

&lt;p&gt;An Execution Context is the environment in which JavaScript executes code. Whenever JavaScript runs a script or calls a function, it creates an execution context.&lt;/p&gt;

&lt;p&gt;Each execution context has two phases:&lt;/p&gt;

&lt;p&gt;i)Memory Creation Phase&lt;br&gt;
     Before executing the code, JavaScript scans it and allocates memory.&lt;/p&gt;

&lt;p&gt;During this phase:&lt;/p&gt;

&lt;p&gt;var variables are initialized with undefined.&lt;br&gt;
let and const are allocated memory but remain uninitialized (Temporal Dead Zone).&lt;br&gt;
Function declarations are stored completely in memory.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;console.log(a);&lt;br&gt;
var a = 10;&lt;br&gt;
Output:&lt;br&gt;
undefined&lt;/p&gt;

&lt;p&gt;Because var is initialized with undefined during the memory phase.&lt;/p&gt;

&lt;p&gt;ii)Code Execution Phase&lt;/p&gt;

&lt;p&gt;After memory allocation, JavaScript starts executing the code line by line.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
var a = 10;&lt;br&gt;
console.log(a);&lt;br&gt;
Output:&lt;br&gt;
10&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variables in JavaScript&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I learned the differences between var, let, and const.&lt;/p&gt;

&lt;p&gt;a)var&lt;br&gt;
-Function scoped&lt;br&gt;
-Can be redeclared&lt;br&gt;
-Can be reassigned&lt;br&gt;
-Hoisted with undefined&lt;/p&gt;

&lt;p&gt;b)let&lt;br&gt;
Block scoped&lt;br&gt;
Cannot be redeclared in the same scope&lt;br&gt;
Can be reassigned&lt;br&gt;
Exists in the Temporal Dead Zone before initialization&lt;/p&gt;

&lt;p&gt;c)const&lt;br&gt;
Block scoped&lt;br&gt;
Cannot be redeclared&lt;br&gt;
Cannot be reassigned&lt;br&gt;
Must be initialized during declaration&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lexical Scope&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One important concept I learned is Lexical Scope.&lt;/p&gt;

&lt;p&gt;Lexical Scope means a function can access variables from the scope where it was created.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;let name = "Harshith";&lt;/p&gt;

&lt;p&gt;function greet() {&lt;br&gt;
    console.log(name);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;greet();&lt;/p&gt;

&lt;p&gt;The function accesses name because it was created inside the same scope.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A Closure is created when a function remembers variables from its outer scope even after the outer function has finished executing.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;function counter() {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let count = 0;

return function () {
    count++;
    console.log(count);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;const increment = counter();&lt;/p&gt;

&lt;p&gt;increment();&lt;br&gt;
increment();&lt;br&gt;
increment();&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
1&lt;br&gt;
2&lt;br&gt;
3&lt;/p&gt;

&lt;p&gt;The variable count remains available because of the closure.&lt;/p&gt;

&lt;p&gt;Closures are commonly used to create private variables and implement the module pattern.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Hoisting&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hoisting is JavaScript's behavior of moving declarations to the top of their scope before execution.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;console.log(a);&lt;br&gt;
var a = 10;&lt;/p&gt;

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

&lt;p&gt;For let and const:&lt;/p&gt;

&lt;p&gt;console.log(a);&lt;br&gt;
let a = 10;&lt;/p&gt;

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

&lt;p&gt;because the variable is inside the Temporal Dead Zone until it is initialized.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Understanding this Binding&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One of the most interesting topics I learned was how JavaScript decides the value of this.&lt;/p&gt;

&lt;p&gt;a)Default Binding&lt;br&gt;
function greet() {&lt;br&gt;
    console.log(this);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;greet();&lt;/p&gt;

&lt;p&gt;In non-strict mode, this refers to the global object.&lt;/p&gt;

&lt;p&gt;b)Implicit Binding&lt;br&gt;
const person = {&lt;br&gt;
    name: "Harshith",&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greet() {
    console.log(this.name);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;person.greet();&lt;/p&gt;

&lt;p&gt;Here, this refers to person.&lt;/p&gt;

&lt;p&gt;c)Explicit Binding&lt;/p&gt;

&lt;p&gt;JavaScript allows us to manually set this using call(), apply(), and bind().&lt;/p&gt;

&lt;p&gt;function greet(city) {&lt;br&gt;
    console.log(this.name, city);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;const person = {&lt;br&gt;
    name: "Harshith"&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;greet.call(person, "Hyderabad");&lt;/p&gt;

&lt;p&gt;d)new Binding&lt;/p&gt;

&lt;p&gt;When a function is called using the new keyword, JavaScript creates a new object and binds this to it.&lt;/p&gt;

&lt;p&gt;function Person(name) {&lt;br&gt;
    this.name = name;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;const p = new Person("Harsh");&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Writing Better Code with DRY and KISS&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Besides JavaScript internals, I also learned two important software engineering principles.&lt;/p&gt;

&lt;p&gt;a)DRY (Don't Repeat Yourself)&lt;/p&gt;

&lt;p&gt;Instead of repeating the same logic multiple times, create reusable functions.&lt;/p&gt;

&lt;p&gt;This makes code easier to maintain.&lt;/p&gt;

&lt;p&gt;b)KISS (Keep It Simple, Stupid)&lt;/p&gt;

&lt;p&gt;Write simple, readable code instead of overly complex solutions.&lt;/p&gt;

&lt;p&gt;Simple code is easier to understand, debug, and maintain.&lt;/p&gt;

&lt;p&gt;Thank you for reading! If you're also learning JavaScript, I hope this summary helps you understand these core concepts more clearly. Happy coding! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>blog</category>
    </item>
  </channel>
</rss>
