How to Pass a JavaScript Interview: Top Questions from A to Z
A JavaScript developer interview is not just a syntax check. Employers want to see a deep understanding of the language, its features, and the ability to work with asynchrony. In this article, we will break down key topics that will help you confidently pass the interview. We will cover questions from basic concepts to advanced Node.js topics with real code examples.
1. Basics: Data Types, Hoisting, and Scope
Let's start with the fundamentals. JavaScript is a dynamically typed language. Understanding var, let, and const is a must-have minimum.
Question: What is the difference between var, let, and const?
varhas function scope and is hoisted with the valueundefined.letandconsthave block scope and are not initialized until declaration (Temporal Dead Zone).constprevents reassignment but does not make the object immutable.
Hoisting example:
console.log(a); // undefined (hoisting)var a = 5;
console.log(b); // ReferenceError: Cannot access 'b' before initializationlet b = 10;Question: What is hoisting?
It is a mechanism where variable and function declarations are moved to the top of their scope before code execution. Functions declared with function are hoisted entirely, while variables are hoisted only in declaration, not initialization.
2. Closures and Context (this)
Closures are one of the strongest features of JavaScript. They allow a function to remember the lexical environment in which they were created.
Question: What is a closure? Provide an example.
A closure is a combination of a function and its lexical environment. In other words, an inner function has access to the variables of an outer function even after the outer function has finished executing.
function createCounter() { let count = 0; return function() { count++; return count; };}
const counter = createCounter();console.log(counter()); // 1console.log(counter()); // 2Question: How does this work in JavaScript?
The value of this is determined by how a function is called, not where it is declared. The main rules are:
- In the global context (in the browser) —
window, in Node.js —global. - In an object method — the object itself.
- In a function called with
new— a new instance. - When using
call,apply,bind— the passed object. - In arrow functions —
thisis taken from the outer lexical context.
const obj = { name: 'Alice', greet() { console.log(this.name); }};
const greet = obj.greet;greet(); // undefined (lost context)
const boundGreet = obj.greet.bind(obj);boundGreet(); // Alice3. Asynchrony: Event Loop, Promises, and async/await
JavaScript is a single-threaded language, but it efficiently handles asynchronous operations thanks to the Event Loop. This is one of the most popular topics in interviews.
Question: What is the Event Loop and how does it work?
The Event Loop is an infinite loop that monitors the Call Stack and task queues (Task Queue / Microtask Queue). If the stack is empty, it takes the first task from the microtask queue (Promise, MutationObserver) or macrotask queue (setTimeout, I/O).
Question: What is the difference between microtasks and macrotasks?
- Microtasks: Promise.then/catch/finally,