Node.js backend interview mistake

The most common Node.js backend interview mistake – answered by the Concise Software!

During many interviews for the position of backend Node.js developer I’ve heard many incorrect answers, but I can distinguish a few of the most common mistakes. One of them is misunderstanding two particular JavaScript concepts: callbacks and closures. Interview questions connected with mentioned topics are one of the most difficult for our candidates. We want to respond to these problems and try to briefly describe the differences between callback and closure.

Doze of the JavaScript history

First of all we need to meet callbacks and understand what they are and what is their purpose and to achieve this we should check the JavaScript history beforehand. As we all probably know JavaScript provides the possibility to write code using asynchronous concepts. At the beginning of the JavaScript history we were not able to use async/await and the whole Promise toolset, because this useful feature has been announced with ECMA 2016. So how people dealt with asynchronous code pieces before? Yes, by using callbacks. 

I’m gonna call you back later on

Callbacks are just regular functions but they are in use in a not very common way. In a classic approach we declare and define a function to use it later. About callbacks there is the same function creation process but these functions are also passed as parameters to other functions. This approach gives us the possibility to dynamically call function from function. Can you see a huge potential here? We can change the implementation of some general function by passing different parameters to the function next calls and that is not all! Callbacks can be automatically called after another function execution is finished. Below we have an example of a callback function.


setTimeout myCallback
We can see here
myCallback() function which is passed as a parameter for the setTimeout(). After three seconds our callback function will be fired and in the console we will be able to see “My Callback function has been fired!” logged string.


Now, let’s go into the closure topic. At the beginning I can say that closures are just functions, like callbacks, but their purpose and place are quite different. JavaScript supports nested functions, so it means that we can define many functions inside a function! According to the variable scopes in JavaScript, the nested function has access to the parent’s environment. We can name a function as a closure function when the function is binded into another one and has access to the parent’s stuff, e.g. variables. 

This feature provides a lot of opportunities to write awesome code. Let’s consider an example. Below we can see the multiplier() function which accepts the first number to make multiplication. Multiplier returns a second function which accepts a second factor for our mathematical operation. At the end we can call multiplier() twice to pass two multiplication’s factors. 


function multiplier

Can callback be closure? 

Here we go: “the killer”. Answer for the headline question is of course simple: yes or no, but if I demand an extended explanation, candidates usually have problems. So officially: callbacks can be closures in JavaScript! Why? Because we can pass a function as an argument to another function and the passed one can access the parent’s environment. Finally: callback which depends on variable context can be called as a closure.

The most common Node.js backend interview mistake – summary

Let’s summarize knowledge from the whole entry. First of all, callbacks are functions which are passed into other ones as arguments and can be fired after the parent’s execution is finished. We can use them to handle asynchronous calls, but nowadays a more convenient way to handle asynchronous calls is to use promises. 

On the other hand we have closures, which are elements bonded into master function and have access to the context environment of their parent. Mentioned before callbacks can be called as closures if they depend on the parent’s context.

Are you ready to work on amazing projects?

This may interest you:

5 reasons to build your backend using Node.js
Node.js vs Python: Which one to choose for your project?
From underground to enterprise – story of the Node.JS
6 examples of enterprise Node.js projects

Web systems developer experienced in the area of small, medium and large ones. Domain Driven Design and Modular Monolith concepts lover. Professes the business first principle. Mainly working with NestJs and serverless framework. Always up to date with astronomy news

You don't have permission to register