So you think you know JavaScript? - DEV Community 👩💻👨💻
<div data-article-id="133678" id="article-body" itemprop="articleBody" readability="288.91309350311">
<p><strong>JavaScript</strong> is an interesting language and we all love it because of its nature. Browsers are the home for JavaScript and both work together at our service.<br/>JS has a few concepts where people tend to take it lightly and sometime may tumble over. Concepts like prototyping, closures and event-loops are still one of those obscure areas where most of the JS developers take a detour. And as we know “little knowledge is a dangerous thing”, it may lead to making mistakes.</p>
<p>Let’s play a mini-game where I am going to ask you a few questions and you have to try answering all of them. Make a guess even if you don’t know the answer or if it’s out of your knowledge. Note down your answers and then check the corresponding answers below. Give yourself a score of 1 for each correct answer. Here we go.</p>
<blockquote readability="8">
<p>Disclaimer: I’ve used ‘var’ purposely in some of the following code snippets so that you guys can copy paste them into your browser’s console without running into SyntaxError problem.<br/>But I don’t condone the usage of ‘var’ declared variables. ‘let’ and ‘const’ declared variables make your code robust and less error-prone.</p>
</blockquote>
<hr/><p><strong>Question 1: What will be printed on the browser console?</strong></p>
<div class="highlight" readability="7"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="kd">function</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span> <span class="c1">// ??</span>
<span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">foo</span><span class="p">();</span>
</code></pre></div>
<p><strong>Question 2: Will output be the same if we use let or const instead of var?</strong></p>
<div class="highlight" readability="7"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="kd">function</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span> <span class="c1">// ??</span>
<span class="kd">let</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">foo</span><span class="p">();</span>
</code></pre></div>
<p><strong>Question 3: What elements will be in the “newArray”?</strong></p>
<div class="highlight" readability="8"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">array</span> <span class="o">=</span> <span class="p">[];</span>
<span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span><span class="mi">3</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">array</span><span class="p">.</span><span class="nx">push</span><span class="p">(()</span> <span class="o">=></span> <span class="nx">i</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">newArray</span> <span class="o">=</span> <span class="nx">array</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">el</span> <span class="o">=></span> <span class="nx">el</span><span class="p">());</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">newArray</span><span class="p">);</span> <span class="c1">// ??</span>
</code></pre></div>
<p><strong>Question 4: If we run the ‘foo’ function in the browser console, will it cause a stack overflow error?</strong></p>
<div class="highlight" readability="8"><pre class="highlight javascript"><code><span class="kd">function</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">setTimeout</span><span class="p">(</span><span class="nx">foo</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span> <span class="c1">// will there by any stack overflow error?</span>
<span class="p">};</span>
</code></pre></div>
<p><strong>Question 5: Will the UI of the page (tab) remains responsive if we run the following function in the console?</strong></p>
<div class="highlight" readability="7"><pre class="highlight javascript"><code><span class="kd">function</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="nb">Promise</span><span class="p">.</span><span class="nx">resolve</span><span class="p">().</span><span class="nx">then</span><span class="p">(</span><span class="nx">foo</span><span class="p">);</span>
<span class="p">};</span>
</code></pre></div>
<p><strong>Question 6: Can we somehow use the spread syntax for the following statement without causing a TypeError?</strong></p>
<div class="highlight" readability="9"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span> <span class="na">x</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="na">y</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="na">z</span><span class="p">:</span> <span class="mi">3</span> <span class="p">};</span>
<span class="p">[...</span><span class="nx">obj</span><span class="p">];</span> <span class="c1">// TypeError</span>
</code></pre></div>
<p><strong>Question 7: What will be printed on the console when we run the following code snippet?</strong></p>
<div class="highlight" readability="14"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span> <span class="na">a</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="na">b</span><span class="p">:</span> <span class="mi">2</span> <span class="p">};</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">setPrototypeOf</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="p">{</span><span class="na">c</span><span class="p">:</span> <span class="mi">3</span><span class="p">});</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="dl">'</span><span class="s1">d</span><span class="dl">'</span><span class="p">,</span> <span class="p">{</span> <span class="na">value</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="na">enumerable</span><span class="p">:</span> <span class="kc">false</span> <span class="p">});</span>
<span class="c1">// what properties will be printed when we run the for-in loop?</span>
<span class="k">for</span><span class="p">(</span><span class="kd">let</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">prop</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div>
<p><strong>Question 8: What value xGetter() will print?</strong></p>
<div class="highlight" readability="9"><pre class="highlight javascript"><code>
<span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">foo</span> <span class="o">=</span> <span class="p">{</span>
<span class="na">x</span><span class="p">:</span> <span class="mi">90</span><span class="p">,</span>
<span class="na">getX</span><span class="p">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="nx">foo</span><span class="p">.</span><span class="nx">getX</span><span class="p">();</span> <span class="c1">// prints 90</span>
<span class="kd">var</span> <span class="nx">xGetter</span> <span class="o">=</span> <span class="nx">foo</span><span class="p">.</span><span class="nx">getX</span><span class="p">;</span>
<span class="nx">xGetter</span><span class="p">();</span> <span class="c1">// prints ??</span>
</code></pre></div>
<hr/><h3>
<a name="answers" href="#answers" class="anchor">
</a>
Answers
</h3>
<p>Now, let’s try to answer each question from top to bottom. I will give you a brief explanation while trying to demystify these behaviors along with some references.</p>
<p><strong>Answer 1:</strong> <em>undefined</em>.<br/><strong>Explanation:</strong> The variables declared with var keywords are <a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" style="color: inherit; text-decoration: none;" name="readabilityLink-1">hoisted</a><a href="#readabilityFootnoteLink-1" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[1]</sup></small></a> in JavaScript and are assigned a value of <em>undefined</em> in the memory. But initialization happens exactly where you typed them in your code. Also, <em>var-declared</em> variables are <a href="http://2ality.com/2011/02/javascript-variable-scoping-and-its.html" style="color: inherit; text-decoration: none;" name="readabilityLink-2">function-scoped</a><a href="#readabilityFootnoteLink-2" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[2]</sup></small></a>, whereas <em>let</em> and <strong>const</strong> have block-scoped. So, this is how the process will look like:</p>
<div class="highlight" readability="9"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span> <span class="c1">// global scope</span>
<span class="kd">function</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
<span class="c1">// Declaration of var a will be hoisted to the top of function.</span>
<span class="c1">// Something like: var a;</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span> <span class="c1">// prints undefined</span>
<span class="c1">// actual initialisation of value 20 only happens here</span>
<span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span> <span class="c1">// local scope</span>
<span class="p">}</span>
</code></pre></div>
<hr/><p><strong>Answer 2:</strong> <em>ReferenceError: a is not defined</em>.<br/><strong>Explanation:</strong> <em>let</em> and <em>const</em> allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. Unlike <em>var</em>, these variables are not hoisted and have a so-called <a href="http://exploringjs.com/es6/chvariables.html#sectemporal-dead-zone" style="color: inherit; text-decoration: none;" name="readabilityLink-3">temporal dead zone</a><a href="#readabilityFootnoteLink-3" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[3]</sup></small></a> (TDZ). Trying to access these variables in <em>TDZ</em> will throw a <em>ReferenceError</em> because they can only be accessed until execution reaches the declaration. Read more about <a href="http://2ality.com/2015/02/es6-scoping.html" style="color: inherit; text-decoration: none;" name="readabilityLink-4">lexical scoping</a><a href="#readabilityFootnoteLink-4" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[4]</sup></small></a> and <a href="http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/" style="color: inherit; text-decoration: none;" name="readabilityLink-5">Execution Context & Stack</a><a href="#readabilityFootnoteLink-5" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[5]</sup></small></a> in JavaScript.</p>
<div class="highlight" readability="11"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span> <span class="c1">// global scope</span>
<span class="kd">function</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span> <span class="c1">// enter new scope, TDZ starts</span>
<span class="c1">// Uninitialised binding for 'a' is created</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span> <span class="c1">// ReferenceError</span>
<span class="c1">// TDZ ends, 'a' is initialised with value of 20 here only</span>
<span class="kd">let</span> <span class="nx">a</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div>
<p>The following table outlines the hoisting behaviour and scoping associated with different keywords used in JavaScript (<em>credit: <a href="https://twitter.com/rauschma" style="color: inherit; text-decoration: none;" name="readabilityLink-6">Axel Rauschmayer</a>'s blog <a href="http://exploringjs.com/es6/chvariables.html#ways-of-declaring-variables" style="color: inherit; text-decoration: none;" name="readabilityLink-7">post</a><a href="#readabilityFootnoteLink-6" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[6]</sup></small></a><a href="#readabilityFootnoteLink-7" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[7]</sup></small></a></em>).</p>
<hr/><p><strong>Answer 3:</strong> <em>[3, 3, 3]</em>.<br/><strong>Explanation:</strong> Declaring a variable with <em>var</em> keyword in the head of <em>for loop</em> creates a single binding (storage space) for that variable. Read more about <a href="http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/" style="color: inherit; text-decoration: none;" name="readabilityLink-8">closures</a><a href="#readabilityFootnoteLink-8" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[8]</sup></small></a>. Let’s look at the for loop one more time.</p>
<div class="highlight" readability="13"><pre class="highlight javascript"><code><span class="c1">// Misunderstanding scope:thinking that block-level scope exist here</span>
<span class="kd">var</span> <span class="nx">array</span> <span class="o">=</span> <span class="p">[];</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="mi">3</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// Every 'i' in the bodies of the three arrow functions</span>
<span class="c1">// referes to the same binding, which is why they all</span>
<span class="c1">// return the same value of '3' at the end of the loop.</span>
<span class="nx">array</span><span class="p">.</span><span class="nx">push</span><span class="p">(()</span> <span class="o">=></span> <span class="nx">i</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">newArray</span> <span class="o">=</span> <span class="nx">array</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">el</span> <span class="o">=></span> <span class="nx">el</span><span class="p">());</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">newArray</span><span class="p">);</span> <span class="c1">// [3, 3, 3]</span>
</code></pre></div>
<p>If you <em>let-declare</em> a variable, which has a <em>block-level</em> scope, a new binding is created for each loop iteration.</p>
<div class="highlight" readability="14"><pre class="highlight javascript"><code><span class="c1">// Using ES6 block-scoped binding</span>
<span class="kd">var</span> <span class="nx">array</span> <span class="o">=</span> <span class="p">[];</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="mi">3</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// This time, each 'i' refers to the binding of one specific iteration</span>
<span class="c1">// and preserves the value that was current at that time.</span>
<span class="c1">// Therefore, each arrow function returns a different value.</span>
<span class="nx">array</span><span class="p">.</span><span class="nx">push</span><span class="p">(()</span> <span class="o">=></span> <span class="nx">i</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">newArray</span> <span class="o">=</span> <span class="nx">array</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">el</span> <span class="o">=></span> <span class="nx">el</span><span class="p">());</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">newArray</span><span class="p">);</span> <span class="c1">// [0, 1, 2]</span>
</code></pre></div>
<p>Another way of solving this quirk would be to use <a href="http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/" style="color: inherit; text-decoration: none;" name="readabilityLink-9">closures</a><a href="#readabilityFootnoteLink-9" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[9]</sup></small></a>.</p>
<div class="highlight" readability="13"><pre class="highlight javascript"><code><span class="c1">// After understanding static scoping and thus closures.</span>
<span class="c1">// Without static scoping, there's no concept of closures.</span>
<span class="kd">let</span> <span class="nx">array</span> <span class="o">=</span> <span class="p">[];</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><</span> <span class="mi">3</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="c1">// invoking the function to capture (closure) the variable's current value in the loop.</span>
<span class="nx">array</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">x</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">x</span><span class="p">;</span>
<span class="p">};</span>
<span class="p">})(</span><span class="nx">i</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">const</span> <span class="nx">newArray</span> <span class="o">=</span> <span class="nx">array</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">el</span> <span class="o">=></span> <span class="nx">el</span><span class="p">());</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">newArray</span><span class="p">);</span> <span class="c1">// [0, 1, 2]</span>
</code></pre></div>
<hr/><p><strong>Answer 4:</strong> <em>No</em>.<br/><strong>Explanation</strong>: JavaScript concurrency model is based on an “event loop”. When I said “Browsers are the home for JS”, what I really meant was that browsers provide runtime environment to execute our JavaScript code. The main components of the browser include <strong>Call stack, Event loop, Task Queue</strong> and <strong>Web APIs</strong>. Globals functions like <em>setTimeout</em>, <em>setInterval</em>, and <em>Promise</em> are not the part of JavaScript but the Web APIs. The visual representation of the JavaScript environment can be something like as shown below:</p>
<p><a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H7B1Ci9B--/climit%2Cfauto%2Cflprogressive%2Cqauto%2Cw880/https://thepracticaldev.s3.amazonaws.com/i/jms55ccv1xaua19uqba8.png" class="article-body-image-wrapper"><img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H7B1Ci9B--/climit%2Cfauto%2Cflprogressive%2Cqauto%2Cw880/https://thepracticaldev.s3.amazonaws.com/i/jms55ccv1xaua19uqba8.png" alt="alt text" title="javascript runtime environment" loading="lazy"/></a></p>
<p>JS call stack is Last In First Out (LIFO). The engine takes one function at a time from the stack and runs the code sequentially from top to bottom. Every time it encounters some asynchronous code, like <em>setTimeout</em>, it hands it over to the Web API (<em>arrow 1</em>). So, whenever an event is triggered, the <em>callback</em> gets sent to the Task Queue (<em>arrow 2</em>).</p>
<p>The Event loop is constantly monitoring the Task Queue and process one <em>callback</em> at a time in the order they were queued. Whenever the call stack is empty, the loop picks up the callback and put it into the stack (<em>arrow 3</em>) for processing. Keep in mind that if call stack is not empty, event loop won’t push any <em>callbacks</em> to the stack.</p>
<p>For a more detailed description of how Event loop works in JavaScript, I highly recommend watching this <a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ" style="color: inherit; text-decoration: none;" name="readabilityLink-10">video</a><a href="#readabilityFootnoteLink-10" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[10]</sup></small></a> by Philip Roberts. Additionally, you can also visualise and understand the call stack via this awesome <a href="http://latentflip.com/loupe/" style="color: inherit; text-decoration: none;" name="readabilityLink-11">tool</a><a href="#readabilityFootnoteLink-11" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[11]</sup></small></a>. Go ahead and run the ‘foo’ function there and see what happens!</p>
<p>Now, armed with this knowledge, let’s try to answer the aforementioned question:</p>
<h4>
<a name="steps" href="#steps" class="anchor">
</a>
Steps
</h4>
<ol><li>Calling <em>foo()</em> will put the <em>foo</em> function into the <strong>call stack</strong>.</li>
<li>While processing the code inside, JS engine encounters the <em>setTimeout</em>.</li>
<li>It then hands over the <em>foo</em> callback to the <strong>WebAPIs</strong> (arrow 1) and returns from the function. The call stack is empty again.</li>
<li>The timer is set to 0, so the foo will be sent to the <strong>Task Queue</strong> (arrow 2).</li>
<li>As, our call stack was empty, the event loop will pick the <em>foo</em> callback and push it to the call stack for processing.</li>
<li>The Process repeats again and <strong>stack doesn't overflow</strong> ever.</li>
</ol><hr/><p><strong>Answer 5:</strong> <em>No</em>.<br/><strong>Explanation</strong>: Most of the time, I have seen developers assuming that we have only one task queue in the event loop picture. But that’s not true. We can have multiple task queues. It’s up to the browser to pick up any queue and processes the <em>callbacks</em> inside.</p>
<p>On a high level, there are macrotasks and microtasks in JavaScript. The <em>setTimeout</em> callbacks are <strong>macrotasks</strong> whereas <em>Promise</em> callbacks are <strong>microtasks</strong>. The main difference is in their execution ceremony. Macrotasks are pushed into the stack one at a time in a single loop cycle, but the microtask queue is always emptied before execution returns to the event loop including any additionally queued items. So, if you were adding items to this queue as quickly you’re processing them, you are processing micro tasks forever. For a more in-depth explanation, watch this <a href="https://www.youtube.com/watch?v=cCOL7MC4Pl0" style="color: inherit; text-decoration: none;" name="readabilityLink-12">video</a><a href="#readabilityFootnoteLink-12" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[12]</sup></small></a> or <a href="https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/" style="color: inherit; text-decoration: none;" name="readabilityLink-13">article</a><a href="#readabilityFootnoteLink-13" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[13]</sup></small></a> by <a href="https://twitter.com/jaffathecake" style="color: inherit; text-decoration: none;" name="readabilityLink-14">Jake Archibald</a><a href="#readabilityFootnoteLink-14" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[14]</sup></small></a>.</p>
<blockquote readability="5">
<p>Microtask queue is always emptied before execution returns to the event loop</p>
</blockquote>
<p>Now, when you run the following code snippet in your console:</p>
<div class="highlight" readability="7"><pre class="highlight javascript"><code><span class="kd">function</span> <span class="nx">foo</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="nb">Promise</span><span class="p">.</span><span class="nx">resolve</span><span class="p">().</span><span class="nx">then</span><span class="p">(</span><span class="nx">foo</span><span class="p">);</span>
<span class="p">};</span>
</code></pre></div>
<p>Every single invocation of ‘foo’ will continue to add another ‘foo’ callback on the microtask queue and thus the event loop can’t continue to process your other events (scroll, click etc) until that queue has completely emptied. Consequently, it blocks the rendering.</p>
<hr/><p><strong>Answer 6:</strong> <em>Yes, by making object <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iterationprotocols" style="color: inherit; text-decoration: none;" name="readabilityLink-15">iterables</a><a href="#readabilityFootnoteLink-15" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[15]</sup></small></a>.</em><br/><strong>Explanation</strong>: The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spreadsyntax" style="color: inherit; text-decoration: none;" name="readabilityLink-16">spread syntax</a><a href="#readabilityFootnoteLink-16" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[16]</sup></small></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of" style="color: inherit; text-decoration: none;" name="readabilityLink-17">for-of</a><a href="#readabilityFootnoteLink-17" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[17]</sup></small></a> statement iterates over data that iterable object defines to be iterated over. Array or Map are built-in iterables with default iteration behaviour. Objects are not iterables, but you can make them iterable by using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iterationprotocols#Theiterableprotocol" style="color: inherit; text-decoration: none;" name="readabilityLink-18">iterable</a><a href="#readabilityFootnoteLink-18" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[18]</sup></small></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iterationprotocols#Theiteratorprotocol" style="color: inherit; text-decoration: none;" name="readabilityLink-19">iterator</a><a href="#readabilityFootnoteLink-19" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[19]</sup></small></a> protocols.</p>
<p>In Mozilla documentation, an object is said to be iterable if it implements the @@iterator method, meaning that the object (or one of the objects up its <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritanceandtheprototypechain" style="color: inherit; text-decoration: none;" name="readabilityLink-20">prototype chain</a><a href="#readabilityFootnoteLink-20" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[20]</sup></small></a>) must have a property with a @@iterator key which is available via constant Symbol.iterator.</p>
<p>The aforesaid statement may seem a bit verbose, but the following example will make more sense:</p>
<div class="highlight" readability="18"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span> <span class="na">x</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="na">y</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="na">z</span><span class="p">:</span> <span class="mi">3</span> <span class="p">};</span>
<span class="nx">obj</span><span class="p">[</span><span class="nb">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="c1">// An iterator is an object which has a next method,</span>
<span class="c1">// which also returns an object with atleast</span>
<span class="c1">// one of two properties: value & done.</span>
<span class="c1">// returning an iterator object</span>
<span class="k">return</span> <span class="p">{</span>
<span class="na">next</span><span class="p">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">countDown</span> <span class="o">===</span> <span class="mi">3</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">const</span> <span class="nx">lastValue</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">countDown</span><span class="p">;</span>
<span class="k">return</span> <span class="p">{</span> <span class="na">value</span><span class="p">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">countDown</span><span class="p">,</span> <span class="na">done</span><span class="p">:</span> <span class="kc">true</span> <span class="p">};</span>
<span class="p">}</span>
<span class="k">this</span><span class="p">.</span><span class="nx">countDown</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">countDown</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">return</span> <span class="p">{</span> <span class="na">value</span><span class="p">:</span> <span class="k">this</span><span class="p">.</span><span class="nx">countDown</span><span class="p">,</span> <span class="na">done</span><span class="p">:</span> <span class="kc">false</span> <span class="p">};</span>
<span class="p">},</span>
<span class="na">countDown</span><span class="p">:</span> <span class="mi">0</span>
<span class="p">};</span>
<span class="p">};</span>
<span class="p">[...</span><span class="nx">obj</span><span class="p">];</span> <span class="c1">// will print [1, 2, 3]</span>
</code></pre></div>
<p>You can also use a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function" style="color: inherit; text-decoration: none;" name="readabilityLink-21">generator</a><a href="#readabilityFootnoteLink-21" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[21]</sup></small></a> function to customize iteration behaviour for the object:</p>
<div class="highlight" readability="12"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span> <span class="na">x</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="na">y</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="na">z</span><span class="p">:</span> <span class="mi">3</span> <span class="p">};</span>
<span class="nx">obj</span><span class="p">[</span><span class="nb">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]</span> <span class="o">=</span> <span class="kd">function</span><span class="o"></span><span class="p">()</span> <span class="p">{</span>
<span class="k">yield</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">yield</span> <span class="mi">2</span><span class="p">;</span>
<span class="k">yield</span> <span class="mi">3</span><span class="p">;</span>
<span class="p">};</span>
<span class="p">[...</span><span class="nx">obj</span><span class="p">];</span> <span class="c1">// print [1, 2, 3]</span>
</code></pre></div>
<hr/><p><strong>Answer 7:</strong> <em>a, b, c</em>.<br/><strong>Explanation</strong>: The for-in loop iterates over the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerabilityandownershipofproperties" style="color: inherit; text-decoration: none;" name="readabilityLink-22">enumerable properties</a><a href="#readabilityFootnoteLink-22" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[22]</sup></small></a> of an object itself and those the object inherits from its prototype. An enumerable property is one that can be included in and visited during for-in loops.</p>
<div class="highlight" readability="14"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span> <span class="na">a</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="na">b</span><span class="p">:</span> <span class="mi">2</span> <span class="p">};</span>
<span class="kd">var</span> <span class="nx">descriptor</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">getOwnPropertyDescriptor</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="dl">"</span><span class="s2">a</span><span class="dl">"</span><span class="p">);</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">descriptor</span><span class="p">.</span><span class="nx">enumerable</span><span class="p">);</span> <span class="c1">// true</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">descriptor</span><span class="p">);</span>
<span class="c1">// { value: 1, writable: true, enumerable: true, configurable: true }</span>
</code></pre></div>
<p>Now having this knowledge in your bag, it should be easy to understand why our code printed those specific properties:</p>
<div class="highlight" readability="19"><pre class="highlight javascript"><code>
<span class="kd">var</span> <span class="nx">obj</span> <span class="o">=</span> <span class="p">{</span> <span class="na">a</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="na">b</span><span class="p">:</span> <span class="mi">2</span> <span class="p">};</span> <span class="c1">// a, b are both enumerables properties</span>
<span class="c1">// setting {c: 3} as the prototype of 'obj', and as we know</span>
<span class="c1">// for-in loop also iterates over the properties obj inherits</span>
<span class="c1">// from its prototype, 'c' will also be visited.</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">setPrototypeOf</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="p">{</span> <span class="na">c</span><span class="p">:</span> <span class="mi">3</span> <span class="p">});</span>
<span class="c1">// we are defining one more property 'd' into our 'obj', but</span>
<span class="c1">// we are setting the 'enumerable' to false. It means 'd' will be ignored.</span>
<span class="nb">Object</span><span class="p">.</span><span class="nx">defineProperty</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="dl">"</span><span class="s2">d</span><span class="dl">"</span><span class="p">,</span> <span class="p">{</span> <span class="na">value</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="na">enumerable</span><span class="p">:</span> <span class="kc">false</span> <span class="p">});</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">obj</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">prop</span><span class="p">);</span>
<span class="p">}</span>
<span class="c1">// it will print</span>
<span class="c1">// a</span>
<span class="c1">// b</span>
<span class="c1">// c</span>
</code></pre></div>
<hr/><p><strong>Answer 8:</strong> <em>10</em>.<br/><strong>Explanation</strong>: When we initialised <em>x</em> into the global scope, it becomes the property of the <em>window</em> object (assuming that it’s a browser environment and not a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strictmode" style="color: inherit; text-decoration: none;" name="readabilityLink-23">strict</a><a href="#readabilityFootnoteLink-23" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[23]</sup></small></a> mode). Looking at the code below:</p>
<div class="highlight" readability="9"><pre class="highlight javascript"><code><span class="kd">var</span> <span class="nx">x</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span> <span class="c1">// global scope</span>
<span class="kd">var</span> <span class="nx">foo</span> <span class="o">=</span> <span class="p">{</span>
<span class="na">x</span><span class="p">:</span> <span class="mi">90</span><span class="p">,</span>
<span class="na">getX</span><span class="p">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">x</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="nx">foo</span><span class="p">.</span><span class="nx">getX</span><span class="p">();</span> <span class="c1">// prints 90</span>
<span class="kd">let</span> <span class="nx">xGetter</span> <span class="o">=</span> <span class="nx">foo</span><span class="p">.</span><span class="nx">getX</span><span class="p">;</span>
<span class="nx">xGetter</span><span class="p">();</span> <span class="c1">// prints 10</span>
</code></pre></div>
<p>We can assert that:</p>
<p><strong>this</strong> will always point to the object onto which the method was invoked. So, in the case of foo.getX(), <strong>this</strong> points to <em>foo</em> object returning us the value of 90. Whereas in the case of <em>xGetter()</em>, <strong>this</strong> points to the <em>window</em> object returning us the value of 10.</p>
<p>To retrieve the value of <em>foo.x</em>, we can create a new function by binding the value of <strong>this</strong> to the <em>foo</em> object using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Globalobjects/Function/bind" style="color: inherit; text-decoration: none;" name="readabilityLink-24">Function.prototype.bind</a><a href="#readabilityFootnoteLink-24" class="readability-DoNotFootnote" style="color: inherit;"><small><sup>[24]</sup></small></a>.</p>
<div class="highlight" readability="7"><pre class="highlight javascript"><code><span class="kd">let</span> <span class="nx">getFooX</span> <span class="o">=</span> <span class="nx">foo</span><span class="p">.</span><span class="nx">getX</span><span class="p">.</span><span class="nx">bind</span><span class="p">(</span><span class="nx">foo</span><span class="p">);</span>
<span class="nx">getFooX</span><span class="p">();</span> <span class="c1">// prints 90</span>
</code></pre></div>
<p>That’s all! Well done if you’ve got all of your answers correct. We all learn by making mistakes. It’s all about knowing the ‘why’ behind it. Know your tools and know them better. If you liked the article, a few ❤️ will definitely make me smile 😀. </p>
<p>What was your score anyway 😃?</p>
</div><div id="readability-footnotes"><h3>References</h3><ol id="readability-footnotes-list"><li><small><sup><a href="#readabilityLink-1" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" name="readabilityFootnoteLink-1">hoisted</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-2" title="Jump to Link in Article">^</a></sup></small> <a href="http://2ality.com/2011/02/javascript-variable-scoping-and-its.html" name="readabilityFootnoteLink-2">function-scoped</a><small> (2ality.com)</small></li><li><small><sup><a href="#readabilityLink-3" title="Jump to Link in Article">^</a></sup></small> <a href="http://exploringjs.com/es6/chvariables.html#sectemporal-dead-zone" name="readabilityFootnoteLink-3">temporal dead zone</a><small> (exploringjs.com)</small></li><li><small><sup><a href="#readabilityLink-4" title="Jump to Link in Article">^</a></sup></small> <a href="http://2ality.com/2015/02/es6-scoping.html" name="readabilityFootnoteLink-4">lexical scoping</a><small> (2ality.com)</small></li><li><small><sup><a href="#readabilityLink-5" title="Jump to Link in Article">^</a></sup></small> <a href="http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/" name="readabilityFootnoteLink-5">Execution Context & Stack</a><small> (davidshariff.com)</small></li><li><small><sup><a href="#readabilityLink-6" title="Jump to Link in Article">^</a></sup></small> <a href="https://twitter.com/rauschma" name="readabilityFootnoteLink-6">Axel Rauschmayer</a><small> (twitter.com)</small></li><li><small><sup><a href="#readabilityLink-7" title="Jump to Link in Article">^</a></sup></small> <a href="http://exploringjs.com/es6/chvariables.html#ways-of-declaring-variables" name="readabilityFootnoteLink-7">post</a><small> (exploringjs.com)</small></li><li><small><sup><a href="#readabilityLink-8" title="Jump to Link in Article">^</a></sup></small> <a href="http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/" name="readabilityFootnoteLink-8">closures</a><small> (dmitrysoshnikov.com)</small></li><li><small><sup><a href="#readabilityLink-9" title="Jump to Link in Article">^</a></sup></small> <a href="http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/" name="readabilityFootnoteLink-9">closures</a><small> (dmitrysoshnikov.com)</small></li><li><small><sup><a href="#readabilityLink-10" title="Jump to Link in Article">^</a></sup></small> <a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ" name="readabilityFootnoteLink-10">video</a><small> (www.youtube.com)</small></li><li><small><sup><a href="#readabilityLink-11" title="Jump to Link in Article">^</a></sup></small> <a href="http://latentflip.com/loupe/" name="readabilityFootnoteLink-11">tool</a><small> (latentflip.com)</small></li><li><small><sup><a href="#readabilityLink-12" title="Jump to Link in Article">^</a></sup></small> <a href="https://www.youtube.com/watch?v=cCOL7MC4Pl0" name="readabilityFootnoteLink-12">video</a><small> (www.youtube.com)</small></li><li><small><sup><a href="#readabilityLink-13" title="Jump to Link in Article">^</a></sup></small> <a href="https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/" name="readabilityFootnoteLink-13">article</a><small> (jakearchibald.com)</small></li><li><small><sup><a href="#readabilityLink-14" title="Jump to Link in Article">^</a></sup></small> <a href="https://twitter.com/jaffathecake" name="readabilityFootnoteLink-14">Jake Archibald</a><small> (twitter.com)</small></li><li><small><sup><a href="#readabilityLink-15" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iterationprotocols" name="readabilityFootnoteLink-15">iterables</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-16" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spreadsyntax" name="readabilityFootnoteLink-16">spread syntax</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-17" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of" name="readabilityFootnoteLink-17">for-of</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-18" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iterationprotocols#Theiterableprotocol" name="readabilityFootnoteLink-18">iterable</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-19" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iterationprotocols#Theiteratorprotocol" name="readabilityFootnoteLink-19">iterator</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-20" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritanceandtheprototypechain" name="readabilityFootnoteLink-20">prototype chain</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-21" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*" name="readabilityFootnoteLink-21">generator</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-22" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerabilityandownershipofproperties" name="readabilityFootnoteLink-22">enumerable properties</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-23" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strictmode" name="readabilityFootnoteLink-23">strict</a><small> (developer.mozilla.org)</small></li><li><small><sup><a href="#readabilityLink-24" title="Jump to Link in Article">^</a></sup></small> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind" name="readabilityFootnoteLink-24">Function.prototype.bind</a><small> (developer.mozilla.org)</small></li></ol></div>