<?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: Ahmed Hasan</title>
    <description>The latest articles on Forem by Ahmed Hasan (@saver711).</description>
    <link>https://forem.com/saver711</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%2F990533%2F3bfa5b02-d8b4-4180-9959-dc99dde701b0.jpeg</url>
      <title>Forem: Ahmed Hasan</title>
      <link>https://forem.com/saver711</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/saver711"/>
    <language>en</language>
    <item>
      <title>Var and Let in for loop with setTimeout - Event loop and block scoping</title>
      <dc:creator>Ahmed Hasan</dc:creator>
      <pubDate>Tue, 20 Dec 2022 16:30:52 +0000</pubDate>
      <link>https://forem.com/saver711/var-and-let-in-for-loop-with-settimeout-event-loop-and-block-scoping-1l72</link>
      <guid>https://forem.com/saver711/var-and-let-in-for-loop-with-settimeout-event-loop-and-block-scoping-1l72</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbpckmnfz32f4n90z5n1b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbpckmnfz32f4n90z5n1b.png" alt="Image description" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Does everyone just tell you that the difference is that Let is block scoped and var is not?&lt;br&gt;
Well, you are not alone - Me too😒.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;I have had enough of these explanations. Let’s really understand it.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This for loop code block requires understanding of “Event loop” and “the differences between block and functional scoping”&lt;br&gt;
I will try to break these concepts down for you first, so you can understand what is going on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Let’s start with the Block scoping. — What is a block in JavaScript code?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A block statement is used to group zero or more statements. The block is delimited by a pair of braces (“curly brackets”)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;➡️➡️ Code Time 🙋‍♂️🥳🎉🎆&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var withVar = 1
let withLet = 1

if (true) {
  var withVar = 2
  let withLet = 2
}

console.log(withVar) //2
console.log(withLet) //1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, why is that? See⬇️⬇️&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var withVar = 1
let withLet = 1

if (true) {
  var withVar = 2
  let withLet = 2
}

console.log(withVar) //2
console.log(withLet) //1

// This code ⬆️⬆️ is equivelat to this ⬇️⬇️

var withVar = 1
let withLet = 1

var withVar = 2 //⬅️⬅️
if (true) {
  let withLet = 2
}

console.log(withVar) //2
console.log(withLet) //1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;strong&gt;But still, why!&lt;/strong&gt;&lt;br&gt;
Well, because the difference between (var / let and const).&lt;br&gt;
_Let _is block scoped and var is not. — *&lt;em&gt;var is functionally or globally scoped&lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This means that in each block, this block creates it’s unique _let _variable.&lt;br&gt;
but with var it declares it globally or functionally if we are inside of a scope of function. &lt;strong&gt;We will see that in coming code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s simplify scoping, local scope and global scope terms.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the simplest way ever: We can dig out, but we can’t dig in :)&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
🙋‍♂️🙋‍♂️ Don't be overwhelmed, See it line by line 👁️👁️
 */
{var withVar = 1}
{let withLet = 2}

console.log(withVar)
// console.log(withLet) // ❌ReferenceError: withLet is not defined - because it is block scoped

//-----------------------------------------------------------------------------------------------------------------------

let globallyDefinedVar = 'globallyDefinedVar data'

function dummyFunc(dummyFuncArg){
    console.log(globallyDefinedVar) // digging out and reaching global scope

    const scopedInDummyFunc = 'scopedInDummyFunc data'
    function innerFunc(){
        console.log(globallyDefinedVar) // digging out and reaching global scope
        console.log(dummyFuncArg) // digging out and reaching dummyFunc scope (it's arguments)
        console.log(scopedInDummyFunc) // digging out and reaching dummyFunc scope

        let scopedInInnerFunc = 'scopedInInnerFunc data'
        var scopedInInnerFuncWithVar = 'scopedInInnerFuncWithVar data'
    }
    innerFunc()
    // console.log(scopedInInnerFunc) // ❌ReferenceError: scopedInInnerFunc is not defined
    // console.log(scopedInInnerFuncWithVar) //❌ReferenceError: scopedInInnerFuncWithVar is not defined - because it is functionally scoped - i can't dig in
}

dummyFunc('dummyFuncArg data')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Let’s simplify event loop, microtask queue, macrotask queue, callback queue, single-threaded and browser Api terms&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I well try to make it short and simple.&lt;br&gt;
I will just focus on event loop with macro task and callback queues here.&lt;br&gt;
if you want farther explanation on how and why event loop deals with asynchronous operations just Let🫡 me know.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;➡️➡️&lt;strong&gt;Time for you to really focus with me :)&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;const foo = () =&amp;gt; console.log("First");
const bar = () =&amp;gt; setTimeout(() =&amp;gt; console.log("Second"), 500);
const baz = () =&amp;gt; console.log("Third");

bar();
foo();
baz();
/**
* First
* Third
* Second
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdchgmm8wb%2Fimage%2Fupload%2Fv1671553409%2Fgif14.1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdchgmm8wb%2Fimage%2Fupload%2Fv1671553409%2Fgif14.1.gif" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keep looking 👁️ at the gif ⬆️⬆️ while reading my explanation.&lt;/p&gt;

&lt;p&gt;In perfect Utopian world where birds can sing, code is running line by line.&lt;br&gt;
but sometimes we encounter an asynchronous operation.&lt;br&gt;
well, JavaScript can not handle 2 operations at once.&lt;br&gt;
there are many ways to make an operation wait until we handle the other one.&lt;/p&gt;

&lt;p&gt;In our case ⬆️ setTimeout is not a built-in JavaScript method. it is provided by the browser.&lt;br&gt;
Event loop keeps on executing Synchronous code until it encounters setTimeout (async), it makes it go to wait in (get handled by) the browser for (n) time.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I say enough talking, Let’s break it down.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;JavaScript has it’s (main/single thread — call stack) where code runs in a sync way (line after line)&lt;/p&gt;

&lt;p&gt;When foo gets executed, it gets added to the call stack, and because it does it’s work immediately by printing “First”, it gets popped out of the stack (in a FILO way).&lt;br&gt;
—&lt;br&gt;
When bar gets executed, it gets added to the call stack.&lt;br&gt;
it has setTimeout method which gets added to the call stack.&lt;br&gt;
“bar is not popped out because it didn’t finish it’s work yet”&lt;br&gt;
setTimeout has a callback function which runs asynchronously.&lt;br&gt;
the browser tells the JavaScript engine: “Hey, This is mine 😠🤬 — i well take care of it for (n) of time then give it back to you. just go help yourself 😠”&lt;br&gt;
—&lt;br&gt;
JavaScript is a good boy “Kinda”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;setTimeout did it’s work, therefore it gets popped out of the stack.&lt;/li&gt;
&lt;li&gt;therefore, bar did it’s work, therefore it gets popped out of the stack.
—
baz gets executed, and gets added to the call stack, and because it does it’s work immediately by printing “Third”, it gets popped out of the stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Do you know what is going on in the background while we are chitchatting here?&lt;br&gt;
I think you do :)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The browser is handling our callback.&lt;br&gt;
after (n) time, it gives it back to us, but it is waiting in the (callback queue / macro tasks queue) until the callstack is empty.&lt;/p&gt;

&lt;p&gt;Now, after the callstack is empty, the callback function gets added to the call stack (in a &lt;a href="https://www.geeksforgeeks.org/fifo-vs-lifo-approach-in-programming" rel="noopener noreferrer"&gt;FIFO &lt;/a&gt;way), and because it does it’s work immediately by printing “Second”, it gets popped out of the stack.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I hope it is all clear now 🫡🫡&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s get back to our main code blocks.&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;for(var i = 0; i &amp;lt; 3; i++){
    setTimeout(()=&amp;gt; console.log(i), 0)
}
//-----------OR-------
var i = 0
for(; i &amp;lt; 3; i++){
    setTimeout(()=&amp;gt; console.log(i), 0)
}
//----------------------------------------------------------
for(let i = 0; i &amp;lt; 3; i++){
    setTimeout(()=&amp;gt; console.log(i), 0)
}
//-----------OR-------
for(let i = 0; i &amp;lt; 3; i++){
    i = i
    setTimeout(() =&amp;gt; console.log(i), 0)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;1️⃣ With var&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Var here is globally scoped right?&lt;/p&gt;

&lt;p&gt;for loop gets executed and added to the call stack (it’s block will run 3 times)&lt;/p&gt;

&lt;p&gt;— — 1st loop&lt;br&gt;
var i = 0 //➡️ it will be attached (hoisted) in the global scope //📝 note that the initialization happens only in first loop, upcoming loops starts from the checking part&lt;br&gt;
0 &amp;lt; 3 ? // true&lt;br&gt;
Do something (our block)&lt;br&gt;
i = i+1&lt;br&gt;
—&lt;br&gt;
setTimeout gets added to the call stack, it does it’s work by sending it’s callback (with the same reference to the globally defined i) to the browser to handle.&lt;br&gt;
Then it gets popped out of the stack.&lt;br&gt;
for loop didn’t finish it’s work yet.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;While the callback is being handled by the browser, it doesn’t have the value of (i).&lt;br&gt;
It has it’s Reference in memory, and because of that when it comes back and get added to the call stack it will tell JS engine: “Give me value of (i), You son of &lt;a href="https://www.google.com/search?sxsrf=ALiCzsarx-z0FmOa8rQhGwRzNczKdNnxhw:1671537418027&amp;amp;q=Brendan+Eich&amp;amp;stick=H4sIAAAAAAAAAONgVuLUz9U3MDE2KrZ8xGjCLfDyxz1hKe1Ja05eY1Tl4grOyC93zSvJLKkUEudig7J4pbi5ELp4FrHyOBWl5qUk5im4ZiZnAAD6oLmNUwAAAA&amp;amp;sa=X&amp;amp;ved=2ahUKEwjB0Z6Qkoj8AhXVWaQEHQpLD-sQzIcDKAB6BAgpEAE" rel="noopener noreferrer"&gt;Brendan Eich&lt;/a&gt; 🤬”&lt;br&gt;
⬆️⬆️these 3 lines of explanations are the whole game changer for me.&lt;br&gt;
If u r interested, I got them while i am in the bathroom🫡.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;— — 2nd loop&lt;br&gt;
1 &amp;lt; 3 ? // true&lt;br&gt;
Do something (our block)&lt;br&gt;
i = i+1&lt;br&gt;
—&lt;br&gt;
setTimeout gets added to the call stack, it does it’s work by sending it’s callback (with the same reference to the globally defined i) to the browser to handle.&lt;br&gt;
Then it gets popped out of the stack.&lt;br&gt;
for loop didn’t finish it’s work yet.&lt;/p&gt;

&lt;p&gt;— — 3rd loop&lt;br&gt;
2 &amp;lt; 3 ? // true&lt;br&gt;
Do something (our block)&lt;br&gt;
i = i+1 — — → it will be 3&lt;br&gt;
—&lt;br&gt;
setTimeout gets added to the call stack, it does it’s work by sending it’s callback (with the same reference to the globally defined i) to the browser to handle.&lt;br&gt;
Then it gets popped out of the stack.&lt;br&gt;
for loop didn’t finish it’s work yet.&lt;/p&gt;

&lt;p&gt;— — 4th loop (Don’t get confused, there will be no 4th loop)&lt;br&gt;
3 &amp;lt; 3 ? // false // ➡️➡️ Remember that last value of (i) is 3&lt;br&gt;
for loop finished it’s work, it gets popped out of the stack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JS Engine to the callback queue: “Hey you, I still have Synchronous work to do, I Will not take your callbacks. not yet”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;With Let&lt;/strong&gt;&lt;br&gt;
Let is block scoped right?&lt;br&gt;
Don’t worry, you will see what this actually means here :)&lt;/p&gt;

&lt;p&gt;for loop gets executed and added to the call stack (it’s block will run 3 times)&lt;/p&gt;

&lt;p&gt;The secret here is: with every iteration (every loop) we are creating a whole new (i), it is not the same i as before.&lt;br&gt;
in second loop We can call it (Ï) or even call it (x).&lt;br&gt;
in third loop We can call it (i with 3 dots 🫡) or even call it (whatever 🫡).&lt;br&gt;
So, when setTimeout callback function takes the reference of (i).&lt;br&gt;
each time it will be a different (i) in memory.&lt;/p&gt;

&lt;p&gt;— — 1st loop&lt;br&gt;
let i = 0 //➡️ it will be attached and hoisted in the block scope of for.&lt;br&gt;
📝 note that the initialization happens only in first loop, upcoming loops starts from the checking part&lt;br&gt;
0 &amp;lt; 3 ? // true&lt;br&gt;
Do something (our block)&lt;br&gt;
i = i+1&lt;br&gt;
—&lt;br&gt;
setTimeout gets added to the call stack, it does it’s work by sending it’s callback with (a whole new reference of i) to the browser to handle.&lt;br&gt;
Then it gets popped out of the stack.&lt;br&gt;
for loop didn’t finish it’s work yet.&lt;/p&gt;

&lt;p&gt;— — 2nd loop&lt;br&gt;
1 &amp;lt; 3 ? // true&lt;br&gt;
Do something (our block)&lt;br&gt;
i = i+1&lt;br&gt;
—&lt;br&gt;
setTimeout gets added to the call stack, it does it’s work by sending it’s callback with (a whole new reference of i) to the browser to handle.&lt;br&gt;
Then it gets popped out of the stack.&lt;br&gt;
for loop didn’t finish it’s work yet.&lt;/p&gt;

&lt;p&gt;— — 3rd loop&lt;br&gt;
2 &amp;lt; 3 ? // true&lt;br&gt;
Do something (our block)&lt;br&gt;
i = i+1&lt;br&gt;
—&lt;br&gt;
setTimeout gets added to the call stack, it does it’s work by sending it’s callback with (a whole new reference of i) to the browser to handle.&lt;br&gt;
Then it gets popped out of the stack.&lt;br&gt;
for loop didn’t finish it’s work yet.&lt;/p&gt;

&lt;p&gt;— — 4th loop (Don’t get confused, there will be no 4th loop)&lt;br&gt;
3 &amp;lt; 3 ? // false // ➡️➡️ last value of (i) is 3 but it won’t matter and we will see.&lt;br&gt;
for loop finished it’s work, it gets popped out of the stack.&lt;/p&gt;




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

&lt;blockquote&gt;
&lt;p&gt;JS Engine to the callback queue: “Hey you, I finished my synchronous code and the call stack is empty, give me the 6 callbacks you have but in a (First In First Out) way, don’t cheat 😒”&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;All first 3 callbacks are coming searching for the reference (i).&lt;br&gt;
Remember?&lt;br&gt;
At this time the (i) the callback is looking for is referring to 3.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvyj9p1s1uytd4ndn1l7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvyj9p1s1uytd4ndn1l7.png" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;br&gt;
This will be done to all 3 callbacks.&lt;/p&gt;

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

&lt;p&gt;As you may expected by now.&lt;br&gt;
every callback of the remaining 3 has a reference to a whole different (i).&lt;/p&gt;

&lt;p&gt;Each callback function will ask for the value of the reference it holds.&lt;br&gt;
So we will have 0, 1, 2&lt;/p&gt;

&lt;p&gt;We will not have 3 because there is no callback calling for a reference that holds a value of 3. ⬇️⬇️&lt;br&gt;
Since the for loop finished before it can send more callbacks to the browser.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;At the end.&lt;/strong&gt;&lt;br&gt;
All JavaScript concepts are connected.&lt;br&gt;
It is a whole process.&lt;br&gt;
For example, our code contains another concept which is Closure.&lt;br&gt;
but i didn’t want to give you headache 😒.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
