Sitemap
Abdul Sayed

Abdul Sayed

Javascript Interview Series Part Two

4 min readJan 12, 2020

In my last article, I presented interview questions that highlighted the scoping and hoisting differences between var vs let/const. I also provided some explanations of the this keyword and situations where you would want to want to explicitly set its value.

In this article, I will introduce direct your attention to some asynchronous concepts.

Spoiler alert — explanation below

Javascript first completes all synchronous code in the scope, skipping over any asynchronous code. Therefore num = 0 + 1 logging 1 first. After the main stack has run, Javascript returns to the synchronous code it set aside. At the time increment() was invoked, num was not yet incremented by 1 (it still had the value of zero). So the async code runs with num = 0 + 2 logging 2.

Spoiler alert — explanation below

setTimeout is always taken off the main stack to be revisited after handling the main synchronous loop. Therefore first to log will be a and then e. However many ms the main loop took to complete will be the time all the async queue stack functions will have been waiting. All set timeouts that had a delay less than the elapsed ms will fire first in the order written. Therefore b logs before d since their 1 ms time difference is made up by the time the synchronous code finishes. So the log order is a, e, b, d, c.

Immediately Invoked Functions

IIFEs are functions that are executed upon creation.

Outputs 10

Currying

Currying is a process in functional programming of transforming a function with multiple arguments into a sequence of nested functions that each takes one of those multiple arguments. Curried functions are invoked repeatedly because each level of nesting returns a function.

A commonly asked question is ‘what is a closure and give an example’.

Closures are nested functions with access to data from outer scope(s). Javascript has lexical scoping which allows variables defined in outer scopes to be available in nested scopes (unlike Ruby).

Example of a closure

Having reviewed IIFE and scoping, we can examine what this will log:

Spoiler alert — explanation below

As we reviewed in the previous article, var is function scoped and maintains its value across the entire f2 function. The for-loop finishes andi increments to 3 long before the first setTimeout executes, so all the setTimeout functions will be using i = 3. Each pass of the for-loop initiates a setTimeout counter. And in each pass, the nested function within the setTimeout is a closure which accesses i to log 3 after a second.

Get Abdul Sayed’s stories in your inbox

Join Medium for free to get updates from this writer.

We correct this by replacing var with let to make i block-scoped, so that i only keeps its value over the for-loop. Therefore setTimeout will only receive the current i of the loop.

logs 0, 1, 2

Another option is to invoke an external function within the for loop, passing it the current i each iteration

logs 0, 1, 2

Yet another option is to encapsulate the nested function of setTimeout and bind the context of the for loop to it, passing in i.

logs 0, 1, 2

--

--