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 is not just about checking syntax, but also about a deep understanding of how the language works. This is especially true for Node.js, where asynchrony and error handling come to the forefront. In this article, we have broken down the top 10 questions that are most often asked in interviews for Junior and Middle developers. Each question is accompanied by a code example and an explanation.



1. What is a closure and where is it used?



A closure is a function that "remembers" its lexical scope even after the outer function has finished executing. This is one of the key concepts of JavaScript.



Example:

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

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


Where is it used? In modules, function factories, memoization, event handlers, and when working with closures in loops (for example, in setTimeout).



2. How does the Event Loop work in Node.js?



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, poll, check, and close callbacks.



Example with microtasks:

console.log('1');

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

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

console.log('4');

// Output: 1, 4, 3, 2


Why is that? Microtasks (Promise) are executed immediately after the current stack and before the next phase of the Event Loop.



3. Difference between == and ===



The == operator compares values with type coercion, while === compares without coercion (strict equality).



console.log(5 == '5');   // true (string coerced to number)console.log(5 === '5');  // false (different types)console.log(null == undefined); // trueconsole.log(null === undefined); // false


In interviews, you are often asked to explain why [] == false gives true (due to abstract comparison with a primitive).



4. What is prototypal inheritance?



In JavaScript, objects can inherit properties from other objects through the prototype chain. Every object has an internal [[Prototype]] property, which can be accessed via __proto__ or Object.getPrototypeOf().



Example:

const animal = { eats: true };const rabbit = { jumps: true };

rabbit.__proto__ = animal;

console.log(rabbit.eats); // true (inherited)console.log(rabbit.jumps); // true


The modern way is to use Object.create() or classes (syntactic sugar over prototypes).



5. How does this work in JavaScript?



The value of this is determined by the context of the function call. The main rules are:

  • In the global context (in the browser) — window, in Node.js — global.
  • In an object method — the object itself.
  • In an arrow function — the lexical this (from the outer function).
  • When called via call/apply/bind — the explicitly passed context.


Example:

const obj = {  name: 'Alice',  greet: function() {    console.log(this.name);  }};

obj.greet(); // Alice

const greet = obj.greet;greet(); // undefined (lost context)

const boundGreet = obj.greet.bind({ name: 'Bob' });boundGreet(); // Bob


6. Promises: chains and error handl

Blogs

Book Recommendations