How to Pass a JavaScript Interview: Top Questions and Answers

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Pass a JavaScript Interview: Top Questions



A JavaScript developer interview (especially with a focus on Node.js) is not just a syntax check. Employers want to see a deep understanding of asynchrony, scopes, and how the Event Loop works. In this article, we have collected the top questions that are actually asked in interviews, with code examples and explanations.



1. Closures — The Foundation of Functional JS



Question: "What is a closure and how does it work in Node.js?"



A closure is a function that "remembers" its lexical environment even after the outer function has finished executing. This is a key mechanism for data encapsulation in JavaScript.



function createCounter() {  let count = 0;  return function() {    count++;    return count;  };}

const counter = createCounter();console.log(counter()); // 1console.log(counter()); // 2


Why this is asked: Closures are the basis of modules, function factories, and event handlers in Node.js. Without understanding closures, it is impossible to work with asynchronous callbacks.



2. Event Loop and Asynchrony (Async/Await, Promises)



Question: "Explain how the Event Loop works. What will this code output?"



console.log('1');

setTimeout(() => console.log('2'), 0);

Promise.resolve().then(() => console.log('3'));

console.log('4');


Answer: 1, 4, 3, 2. First synchronous code, then microtasks (Promise.then), then macrotasks (setTimeout).



The Event Loop is a mechanism that allows Node.js to perform non-blocking operations despite being single-threaded. It consists of several phases:

  • Timers — executes setTimeout/setInterval callbacks
  • I/O callbacks — handles completed input/output operations
  • Poll — retrieves new I/O events
  • Check — executes setImmediate()
  • Close callbacks — closes connections


Tip: In an interview, show an understanding of the difference between process.nextTick() and setImmediate(). nextTick executes before the next phase of the Event Loop, while setImmediate executes in the Check phase.



3. Execution Context (this) and Arrow Functions



Question: "What does this equal in different situations? How do you change the context?"



The value of this is determined by how a function is called, not where it is declared. In Node.js (strict mode), in a regular function, this will be undefined, while in a browser it will be window.



const obj = {  name: 'Node.js',  regularFunc: function() {    console.log(this.name);  },  arrowFunc: () => {    console.log(this.name);  }};

obj.regularFunc(); // 'Node.js'obj.arrowFunc(); // undefined (this is taken from the outer context)


How to manage context:

  • call(thisArg, arg1, arg2) — calls a function with a specified this and arguments
  • apply(thisArg, [args]) — similar, but arguments are passed as an array
  • bind(thisArg) — creates a new function with a bound this
  • Arrow functions — do not have their own this, they use the lexical context


4. Prototypes and Inheritance (Prototype Chain)



Question: "How does prototypal inheritance work in JavaScript? What is the difference from classes?"



JavaScript uses prototypal inheritance: every object has an internal reference [[Prototype]] (accessible via __proto__ or Object.getPrototypeOf()).



function Animal(name) {  this.name = name;}

Animal

Blogs

Book Recommendations