JavaScript Interview Questions

Blandet

Ace your JavaScript interviews with questions on closures, promises, prototypes, the event loop, ES6+ features, and core language concepts.

15 spørsmål|
5 lett
6 middels
4 vanskelig

A closure is a function that retains access to variables from its outer (enclosing) scope even after that outer function has finished executing. This happens because JavaScript functions form a lexical scope chain at creation time. Closures are commonly used for data privacy, factory functions, and maintaining state in callbacks.

closuresscope

var is function-scoped, hoisted to the top of its function, and can be redeclared. let is block-scoped, hoisted but not initialized (temporal dead zone), and cannot be redeclared in the same scope. const is also block-scoped and must be initialized at declaration, though the value it points to (if an object or array) can still be mutated.

variableses6

The event loop continuously checks the call stack and, when it is empty, picks the next task from the task queue (macrotasks) to execute. Microtasks (such as Promise callbacks) have higher priority and are processed entirely before the next macrotask. This single-threaded model allows JavaScript to handle asynchronous operations like I/O, timers, and network requests without blocking the main thread.

event-loopasync

In JavaScript, objects can inherit properties and methods directly from other objects through the prototype chain. Every object has an internal [[Prototype]] link, and when a property is not found on the object itself, JavaScript traverses the chain until it finds the property or reaches null. ES6 classes are syntactic sugar over this prototypal inheritance mechanism.

prototypesoop

The == operator performs type coercion before comparing, so '5' == 5 returns true. The === operator checks both value and type without coercion, so '5' === 5 returns false. It is generally recommended to use === to avoid unexpected type conversion bugs.

operatorsbasics

Promises represent the eventual result of an asynchronous operation, with states of pending, fulfilled, or rejected. They provide .then(), .catch(), and .finally() methods for chaining. Async/await is syntactic sugar over Promises that makes asynchronous code look synchronous: async functions always return a Promise, and await pauses execution until the Promise resolves.

promisesasync

undefined means a variable has been declared but not assigned a value, and it is also the default return value of functions without a return statement. null is an intentional assignment that represents the deliberate absence of a value. Notably, typeof undefined returns 'undefined' while typeof null returns 'object', which is a well-known language quirk.

typesbasics

WeakMap and WeakSet hold weak references to their keys (WeakMap) or values (WeakSet), meaning the entries do not prevent garbage collection of the referenced objects. They are useful for associating metadata with objects without causing memory leaks. Unlike Map and Set, they are not iterable and do not have a size property.

data-structuresmemory

The value of this depends on how a function is called: in a method call it refers to the object, in a regular function call it is undefined (strict mode) or the global object, and in an arrow function it is lexically inherited from the enclosing scope. The bind, call, and apply methods can explicitly set this. Constructor calls with new set this to the newly created instance.

thisscope

Event delegation is a pattern where a single event listener is attached to a parent element instead of individual listeners on each child. It works because events bubble up through the DOM, so the parent can catch events from descendants and use event.target to identify the source. This approach improves performance and automatically handles dynamically added elements.

domevents

Generators are functions declared with function* that can pause and resume execution using the yield keyword. Each call to the generator's next() method runs the function until the next yield and returns an object with value and done properties. They are useful for lazy evaluation, implementing iterators, and managing asynchronous flows.

generatorsiterators

Hoisting is JavaScript's behavior of moving declarations to the top of their scope during compilation. Variable declarations with var are hoisted and initialized to undefined, while let and const are hoisted but remain in a temporal dead zone until their declaration is reached. Function declarations are fully hoisted with their body, but function expressions are not.

hoistingscope

map transforms each element of an array by applying a function and returns a new array of the same length. filter returns a new array containing only elements that pass a test function. reduce iterates over the array and accumulates a single output value by applying a reducer function with an accumulator.

arraysfunctional

Proxy allows you to create a wrapper around an object that intercepts and customizes fundamental operations like property access, assignment, and function invocation using trap handlers. Reflect is a companion object that provides default implementations of the same operations, making it easier to forward behavior from within proxy traps. Together they enable powerful patterns like validation, logging, and reactive programming.

proxymetaprogramming

A shallow copy duplicates only the top-level properties, so nested objects and arrays still reference the same memory as the original. Methods like Object.assign() and the spread operator create shallow copies. A deep copy recursively clones all nested structures, achievable via structuredClone(), JSON.parse(JSON.stringify()), or libraries like Lodash's cloneDeep.

objectsdata-structures