JavaScript Interview Questions for Freshers
The JavaScript questions freshers are most often asked, with a short answer for each. Every question links to a full lesson with the traps interviewers look for and a step-by-step trace of the code.
43 JavaScript interview questions for freshers, with answers
01 · JavaScript Fundamentals for Frontend Interviews · Easy
Primitive vs Reference Types
Data types in JavaScript split into primitives and reference types: primitives are immutable values stored directly by value, whereas reference types (objects, arrays, functions) are mutable entities stored on the heap and accessed via memory references.
Full answer, traps and step-by-step trace02 · JavaScript Fundamentals for Frontend Interviews · Medium
Type Coercion & Equality
Type coercion is JavaScript's automatic or implicit conversion of values between data types during operations.
Full answer, traps and step-by-step trace03 · JavaScript Fundamentals for Frontend Interviews · Medium
Scope, Hoisting & TDZ
Hoisting in JavaScript is the engine's behavior during compilation where variable and function declarations are registered in their environment records before code executes.
Full answer, traps and step-by-step trace04 · JavaScript Fundamentals for Frontend Interviews · Medium
Objects, Arrays & Reference Semantics
Shallow copy vs deep copy in JavaScript starts from one fact: objects and arrays are reference types stored in heap memory, where variable assignments create memory aliases.
Full answer, traps and step-by-step trace05 · JavaScript Fundamentals for Frontend Interviews · Medium
Equality, Coercion & Comparison Traps
JavaScript defines four distinct equality algorithms: loose equality (==), strict equality (===), SameValueZero (used by Map, Set, and includes), and SameValue (used by Object.is).
Full answer, traps and step-by-step trace06 · JavaScript Fundamentals for Frontend Interviews · Easy
Callback Functions
A callback function in JavaScript is a function passed as an argument to another function, which calls it back later.
Full answer, traps and step-by-step trace07 · JavaScript Fundamentals for Frontend Interviews · Easy
for...in vs for...of Loops
The for in loop in JavaScript walks the enumerable string keys of an object, including keys it inherits through the prototype chain. Own integer-like keys come first in ascending order, then other string keys in insertion order, then inherited ones.
Full answer, traps and step-by-step trace08 · JavaScript Fundamentals for Frontend Interviews · Easy
String Methods
String methods in JavaScript, such as slice, substring, split, indexOf, replace, replaceAll, trim, includes and at, all return a new value: strings are immutable, so no method changes the original string.
Full answer, traps and step-by-step trace09 · JavaScript Fundamentals for Frontend Interviews · Easy
Higher Order Functions
Higher order functions in JavaScript are functions that take another function as an argument, return a function, or both. map, filter, reduce, setTimeout and addEventListener take functions; function factories and decorators return them.
Full answer, traps and step-by-step trace10 · JavaScript Fundamentals for Frontend Interviews · Easy
Arrays
An array in JavaScript is an ordered, zero-indexed list of values; it is an object with numeric keys and a length property, so typeof gives 'object' and Array.isArray is the real check.
Full answer, traps and step-by-step trace11 · JavaScript Fundamentals for Frontend Interviews · Easy
Loops
Loops in JavaScript repeat code: for runs a counter loop, while repeats while a condition holds, do...while runs its body at least once, for...of walks the values of an iterable and for...in walks an object's keys.
Full answer, traps and step-by-step trace12 · JavaScript Fundamentals for Frontend Interviews · Easy
Operators
Operators in JavaScript include arithmetic (+, -, *, /, %, **), assignment, comparison, logical (&&, ||, !, ??), optional chaining (?.) and typeof.
Full answer, traps and step-by-step trace13 · JavaScript Fundamentals for Frontend Interviews · Easy
Functions
Functions in JavaScript are reusable blocks of code that are also values. A function declaration is hoisted with its body, so it can be called before its line; a function expression or arrow function follows the rules of the variable holding it.
Full answer, traps and step-by-step trace14 · JavaScript Fundamentals for Frontend Interviews · Easy
Variables
Variables in JavaScript are declared with var, let or const. var is function-scoped and starts as undefined when hoisted; let and const are block-scoped and throw a ReferenceError if read before their line.
Full answer, traps and step-by-step trace15 · JavaScript Fundamentals for Frontend Interviews · Easy
Objects
Objects in JavaScript are collections of key-value pairs whose keys are strings or symbols. Variables hold references to objects, so assigning or passing an object shares it and === compares references, not contents.
Full answer, traps and step-by-step trace16 · Execution Contexts, Closures, and Object Prototypes · Medium
Execution Context & Call Stack
A JavaScript Execution Context is an internal runtime environment that tracks code evaluation, consisting of a Lexical Environment, Variable Environment, and ThisBinding.
Full answer, traps and step-by-step trace17 · Execution Contexts, Closures, and Object Prototypes · Medium
Closures & Lexical Scope
Closures in JavaScript bundle a function with references to its surrounding lexical environment (its [[Environment]] internal slot).
Full answer, traps and step-by-step trace18 · Execution Contexts, Closures, and Object Prototypes · Medium
this, call, apply & bind
The this keyword in JavaScript represents the evaluation context of a function call, determined dynamically by the call-site: default binding (global/undefined), implicit binding (object before the dot), or explicit binding (via call, apply, or bind).
Full answer, traps and step-by-step trace19 · Execution Contexts, Closures, and Object Prototypes · Medium
Prototypes & Inheritance
Every object has a prototype in JavaScript: inheritance works through prototype delegation, where each object holds an internal [[Prototype]] link to another object.
Full answer, traps and step-by-step trace20 · Execution Contexts, Closures, and Object Prototypes · Medium
Property Flags & Descriptors
JavaScript property descriptors govern property behavior through attribute flags: value, writable, enumerable, and configurable.
Full answer, traps and step-by-step trace21 · Execution Contexts, Closures, and Object Prototypes · Easy
IIFE: Immediately Invoked Function Expressions
An IIFE in JavaScript (Immediately Invoked Function Expression) is a function expression that runs as soon as it is defined: (function () { ... })().
Full answer, traps and step-by-step trace22 · The Event Loop, Microtasks, and Asynchronous Concurrency · Medium
Promises & Chaining
Promises in JavaScript represent the eventual completion or failure of an asynchronous operation; each one exists in one of three states: pending, fulfilled, or rejected, and promise chaining builds on that.
Full answer, traps and step-by-step trace23 · The Event Loop, Microtasks, and Asynchronous Concurrency · Medium
async/await Internals
async/await is syntactic sugar over Promises and Generators that allows writing asynchronous code with synchronous control flow.
Full answer, traps and step-by-step trace24 · The Event Loop, Microtasks, and Asynchronous Concurrency · Medium
Promise Combinators (all, allSettled, race, any)
Promise combinators coordinate collections of concurrent promises: Promise.all requires all to fulfill and fails fast on the first rejection; Promise.allSettled waits for all outcomes; Promise.race settles with the first settled promise; and Promise.any…
Full answer, traps and step-by-step trace25 · The Event Loop, Microtasks, and Asynchronous Concurrency · Medium
Timers & Concurrency Control
setTimeout in JavaScript and setInterval differ in one way: both schedule callbacks in the macrotask queue to execute after a minimum delay once the call stack is clear, but setTimeout runs once and setInterval repeats.
Full answer, traps and step-by-step trace26 · The Event Loop, Microtasks, and Asynchronous Concurrency · Medium
Node.js Event Loop
The Node.js event loop is libuv's loop that runs callbacks in phases: timers, pending callbacks, poll (I/O), check (setImmediate) and close callbacks.
Full answer, traps and step-by-step trace27 · Tricky JavaScript Output Prediction & Code Reasoning · Medium
Hoisting & Scope Traps
Function declarations hoist with their complete implementation and take precedence over var declarations during compilation.
Full answer, traps and step-by-step trace28 · JavaScript Utility Implementations and Standard Polyfills · Medium
Debounce and Throttle Implementations
Debouncing in JavaScript delays function execution until a quiet period passes without new invocations, ideal for search autocomplete. Throttle limits function execution to at most once per time window, ideal for scroll, resize, and mousemove handlers.
Full answer, traps and step-by-step trace29 · JavaScript Utility Implementations and Standard Polyfills · Medium
Flattening Nested Arrays
To flatten a nested array in JavaScript is to reduce a multi-dimensional array into a single-level array.
Full answer, traps and step-by-step trace30 · JavaScript Utility Implementations and Standard Polyfills · Medium
Higher-Order Functions & Currying
Currying in JavaScript transforms a function of N arguments into a chain of unary functions via closures, executing when all arguments are collected.
Full answer, traps and step-by-step trace31 · JavaScript Utility Implementations and Standard Polyfills · Medium
JavaScript Array Methods
Custom array polyfills replicate native ECMAScript array methods: map returns a new array of transformed elements, filter returns elements satisfying a predicate, and reduce accumulates elements into a single value while properly handling sparse arrays and…
Full answer, traps and step-by-step trace32 · Browser APIs, DOM Performance, and Client-Side Security · Medium
DOM Events and Event Delegation
Events in JavaScript propagate in three sequential phases, which event delegation builds on: capturing (window down to target), target, and bubbling (target up to window).
Full answer, traps and step-by-step trace33 · Browser APIs, DOM Performance, and Client-Side Security · Medium
DOM Traversal & Reflow
The DOM (Document Object Model) in JavaScript is the browser's tree of nodes for the page: DOM traversal navigates those nodes, while reflow is the browser's expensive calculation of element geometry and layout.
Full answer, traps and step-by-step trace34 · Browser APIs, DOM Performance, and Client-Side Security · Medium
Web Storage, Cookies & Persistence
localStorage in JavaScript, like sessionStorage (together the Web Storage API), stores client-side key-value string pairs without sending data to servers, while HTTP Cookies store small snippets that are transmitted with every HTTP request.
Full answer, traps and step-by-step trace35 · Modern JavaScript: ES Modules, Iterators, and Weak Collections · Medium
JavaScript Modules (ESM vs CommonJS)
ES Modules use live bindings where imported identifiers reflect runtime changes in the exporting module, whereas CommonJS copies primitive values into module.exports.
Full answer, traps and step-by-step trace36 · Modern JavaScript: ES Modules, Iterators, and Weak Collections · Medium
Iterators & Generators
JavaScript iterators and generators share one protocol: an iterator implements a next() method returning { value, done }.
Full answer, traps and step-by-step trace37 · Modern JavaScript: ES Modules, Iterators, and Weak Collections · Medium
Maps, Sets, WeakMap & WeakSet
Map supports arbitrary key types and maintains insertion order, while Set stores unique values using SameValueZero equality.
Full answer, traps and step-by-step trace38 · Modern JavaScript: ES Modules, Iterators, and Weak Collections · Medium
JavaScript Classes & Inheritance
JavaScript classes and inheritance are syntactic sugar over prototype delegation, introducing strict mode execution, class TDZ, and non-enumerable prototype methods.
Full answer, traps and step-by-step trace39 · Modern JavaScript: ES Modules, Iterators, and Weak Collections · Medium
Tagged Templates & Frontend Security
Tagged template literals pass template strings and interpolated expressions to a tag function before string assembly, enabling automatic context-aware escaping to prevent DOM XSS vulnerabilities before injecting dynamic values into the DOM.
Full answer, traps and step-by-step trace40 · Modern JavaScript: ES Modules, Iterators, and Weak Collections · Easy
Arrow Functions
An arrow function in JavaScript is a shorter function syntax, (a, b) => a + b, that does not have its own this, arguments or prototype: it uses the this of the code around it.
Full answer, traps and step-by-step trace41 · Modern JavaScript: ES Modules, Iterators, and Weak Collections · Easy
Spread Operator & Rest Parameters
The spread operator in JavaScript, three dots (...), expands an iterable such as an array or string into individual elements, or an object's own enumerable properties into a new object. It makes a shallow copy: nested objects are still shared.
Full answer, traps and step-by-step trace42 · Modern JavaScript: ES Modules, Iterators, and Weak Collections · Easy
Destructuring Assignment
Destructuring in JavaScript unpacks values from arrays (by position) or properties from objects (by name) into variables in one statement.
Full answer, traps and step-by-step trace43 · Debugging, Memory Leak Analysis, and Code Quality · Medium
Error Handling & Debugging
Try catch in JavaScript is how exception handling works: try/catch/finally blocks manage runtime errors.
Full answer, traps and step-by-step trace
Applying for the role too? Frontend developer resume examples
