JavaScript interview questions focus on a few core ideas: types and equality, scoping and hoisting, closures, this, prototypes and classes, asynchronous code with promises and async/await, and the modern ES2015 to ES2023 syntax. The answers below are written for current browsers and Node.js.
JavaScript is a high-level, dynamically typed language standardised as ECMAScript. It runs in every browser and, through runtimes such as Node.js, Deno and Bun, on servers and tooling. It is not "only for small scripts" and it is not compiled ahead of time by you, although engines use just-in-time compilation for speed.
JavaScript has seven primitive types (string, number, bigint, boolean, undefined, null, symbol) and objects (including arrays, functions, dates and maps). Primitives are immutable and compared by value; objects are compared by reference.
Declare variables with const by default and let when you need to reassign. Avoid var.
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Reassignable | Yes | Yes | No (binding), object contents can change |
| Hoisting | Hoisted, initialised as undefined | Hoisted, in the temporal dead zone until declared | Same as let |
| Redeclaration in same scope | Allowed | Error | Error |
== versus ===. === compares without type coercion. == coerces first, so 0 == "0" is true and 0 === "0" is false. Use === (and Object.is for edge cases such as NaN).
null versus undefined. undefined means a value was never assigned. null is an explicit "no value". typeof null is "object", a long-standing historical quirk.
The nullish coalescing operator ?? falls back only on null and undefined, while || falls back on any falsy value. Optional chaining ?. returns undefined instead of throwing when part of a path is missing.
Hoisting means declarations are processed before code runs. Function declarations are fully hoisted, so you can call them before they appear. let and const are hoisted but cannot be read before their declaration (temporal dead zone), which throws a ReferenceError.
A closure is a function that keeps access to the variables of the scope where it was created, even after that scope has finished.
Closures power private state, callbacks, memoization and partial application. Because let creates a new binding per loop iteration, closures inside a for (let i ...) loop capture the value you expect, unlike the old var version.
A declaration (function add() {}) is hoisted completely. An expression (const add = function () {} or an arrow function) is only available after the line runs. Arrow functions have no own this, no arguments object and cannot be used as constructors.
this depends on how a function is called: the object before the dot for a method call, undefined in strict mode for a plain call, the new object for new, and whatever you pass to call, apply or bind. Arrow functions take this from the surrounding scope.
JavaScript uses prototype-based inheritance. The class syntax is a cleaner way to write the same mechanism.
for is the general loop; while checks the condition first, do...while runs the body at least once.forEach runs a callback per element but cannot be stopped with break and ignores await. Use for...of when you need break, continue or await.map, filter, reduce, find and some express transformations without mutation.toSorted, toReversed, with and findLast return new arrays instead of mutating (ES2023).JavaScript runs your code on a single thread with an event loop. Asynchronous work (timers, network, file I/O) finishes later and its callbacks are queued. Promise callbacks run in the microtask queue, before the next timer callback.
Use Promise.all for parallel work that must all succeed, Promise.allSettled when you want every result, and Promise.race or AbortController for timeouts and cancellation. Top-level await works in ES modules.
Use ES modules (import and export) in browsers and modern Node.js. CommonJS (require) is still common in older Node.js projects. TypeScript, ESLint and a bundler such as Vite are standard in most teams, and npm or pnpm manages packages.
Engines such as V8 parse the code and compile hot paths to machine code just in time, so it is neither purely interpreted nor compiled ahead of time by the developer.
They are unrelated languages that share part of a name. Java is a statically typed, class-based language on the JVM, while JavaScript is dynamically typed and runs in engines such as V8.
Your code runs on one thread per realm, with concurrency provided by the event loop. Use Web Workers or worker_threads for CPU-heavy work.
typeof null return "object"?It is a bug from the first implementation that cannot be fixed without breaking the web. Check for null with value === null.
Know the primitive types, strict equality, let and const scoping, closures, this rules, classes and prototypes, and promises with async/await. Practise writing short, modern snippets such as counters, array pipelines and parallel fetches, because interviewers usually ask you to write them.