JavaScript Interview Questions and Answers
Vanilla JS: types, closures, this, prototypes, async code and the event loop, DOM and browser APIs.
225 questions for JavaScript interviews with answers and explanations - Junior, Middle and Senior levels. Sample questions, online quizzes and coding tasks.
- Closures
- Event loop
- Promises
- Prototypes
- DOM
Questions by level
Junior
Core concepts and syntax · 50 questions
Middle
Practice, nuances and optimization · 50 questions
Senior
Architecture, internals, edge cases · 50 questions
Sample questions
A few questions from each level with the correct answer and an explanation.
JavaScript · Junior
- How many data types does JavaScript have according to the specification?
Correct answer: 8: seven primitives and
objectPrimitives:
number,bigint,string,boolean,null,undefined,symbol. Everything else isobject, including arrays, functions and dates. Functions are objects, even thoughtypeofreturns'function'for them. - What does
typeof nullreturn?Correct answer:
'object'It's a historical bug from the language's first implementation, kept for compatibility.
nullis a separate primitive. Check for it with strict equality:value === null. - What does
typeof NaNreturn?Correct answer:
'number'NaN(Not-a-Number) is a special value of the number type, the result of an invalid math operation such as0 / 0orNumber('abc'). Check for it withNumber.isNaN(x). - How does
letdiffer fromvar?Correct answer:
letis scoped to a{}block,varto a functionA
vardeclared inside aniforforis visible throughout the function. Also,varis hoisted with the valueundefinedand becomes awindowproperty at the top level, whileletandconstare in the "temporal dead zone" until declared.
JavaScript · Middle
- How do
call,applyandbinddiffer from each other?Correct answer:
call/applyinvoke immediately,bindreturns a new functionfn.call(ctx, a, b)andfn.apply(ctx, [a, b])invoke immediately with the giventhis.fn.bind(ctx, a)returns a function with a permanently bound context and partially applied arguments. None of them affect arrow functions: they have no ownthis. - How does the engine look up a property the object itself doesn't have?
Correct answer: It walks up the prototype chain until it finds the property or reaches
nullEvery object has a hidden
[[Prototype]]link (Object.getPrototypeOf). That's how arrays getmapfromArray.prototypeand all objects gettoStringfromObject.prototype. Scope lookup applies to variables, not properties. - What does
Object.create(proto)do?Correct answer: Creates a new empty object whose prototype is
protoThe properties of
protoaren't copied - they're accessible through the prototype chain.Object.create(null)creates an object with no prototype at all: a handy "clean" dictionary without inherited keys liketoString. - What are classes in JavaScript really?
Correct answer: Syntax on top of prototypal inheritance with a few extra checks
Class methods go into
Class.prototype, andextendsbuilds the prototype chain. But there are differences from constructor functions: a class can't be called withoutnew, its body runs in strict mode, methods are non-enumerable, and there are private#xfields andstatic.
JavaScript · Senior
- How does the event loop process tasks in the browser?
Correct answer: One macrotask, then the whole microtask queue, then a possible render
After each macrotask (script, event, timer), the microtask queue is drained completely, including microtasks added along the way. Then the browser may update rendering (calling
requestAnimationFrame) if it's time for a frame. That's why a long chain of microtasks also blocks rendering. - How do you globally track unhandled promise rejections in the browser?
Correct answer: The
unhandledrejectionevent onwindowwindow.addEventListener('unhandledrejection', e => report(e.reason))is the standard way to send such errors to monitoring. Synchronous unhandled errors are caught by theerrorevent onwindow. There's noPromise.onErrormethod. - What does
for await...ofdo?Correct answer: Iterates over an async iterable, awaiting each value
It works with objects that have
Symbol.asyncIterator: async generators, streams (ReadableStream), API pagination. Iterations run sequentially. To process an array of promises in parallel, usePromise.all. - What is
Proxyused for?Correct answer: To intercept operations on an object: reads, writes, deletions
Traps such as
get,set,hasanddeletePropertylet you implement reactivity (that's how Vue 3 works), validation, logging and default values.Reflectprovides the default behavior of operations for use inside traps.
Combined quizzes with this topic
Questions from several topics at once - just like an interview for a specific role.
Coding tasks
Write your solution right in the browser, and tests check it instantly. Interview classics: debounce, throttle, deepClone, Promise.all, EventEmitter, an LRU cache and more.
Open tasks →