JavaScript Interview Questions and Answers
Last updated 2026-11-19
JavaScript interviews open with fundamentals because the language has more surprising edge cases than most. Interviewers move quickly from definitions such as the difference between let and var to snippets where type coercion or hoisting produces an unexpected result.
The questions here cover the core semantics that every fresher and early-career developer should explain with confidence: variable declarations, types, equality, truthiness, copying and the operators that change behaviour with null and undefined. Answers assume modern JavaScript, ES2020 or later.
| Level | Fresher to 2 years |
|---|---|
| Focus | Core language semantics |
| Runtime | Browser and Node.js |
What interviewers listen for
Precise vocabulary matters: primitive versus object, reference versus value, falsy values, coercion. When you predict output, say which rule applies, such as the plus operator preferring string concatenation when either side is a string.
- Seven primitive types: string, number, bigint, boolean, undefined, null, symbol
- Everything else is an object, including arrays and functions
- Prefer === and Object.is over loose equality
Go deeper by topic
Each area has its own page: closures, promises and async/await, the event loop and hoisting and scope. For the round-by-round plan, read how to crack a JavaScript interview.
JavaScript Interview Questions and Answers: questions with worked answers
1 What is the difference between var, let and const?
easyvar is function-scoped, hoisted and initialised to undefined, and can be redeclared. let and const are block-scoped and hoisted but kept in the temporal dead zone until their declaration runs, so using them earlier throws ReferenceError. const requires an initial value and cannot be reassigned, but an object it holds can still be mutated. Prefer const by default and let when reassignment is needed.
javascriptconsole.log(a); // undefined var a = 1; console.log(b); // ReferenceError: Cannot access 'b' before initialization let b = 2; const o = { n: 1 }; o.n = 2; // allowed o = {}; // TypeError: Assignment to constant variable.2 What are the data types in JavaScript, and what does typeof return?
easyThe primitives are string, number, bigint, boolean, undefined, null and symbol, and everything else is an object. typeof returns a string naming the type, with two historical quirks: typeof null is 'object', a long-standing bug kept for compatibility, and typeof of a function is 'function' even though functions are objects. Use Array.isArray to detect arrays.
javascriptconsole.log(typeof 42, typeof "s", typeof undefined); // number string undefined console.log(typeof null, typeof [], typeof function(){}); // object object function console.log(typeof NaN, typeof 10n); // number bigint3 Explain == versus === and predict these comparisons.
medium=== compares type and value with no conversion. == converts operands first following the abstract equality rules, which makes results hard to reason about, so most style guides ban it except for the x == null idiom that matches both null and undefined. NaN is never equal to anything including itself, so use Number.isNaN or Object.is.
javascriptconsole.log(0 == "0", 0 == "", "0" == ""); // true true false console.log(null == undefined, null === undefined); // true false console.log(null == 0, null >= 0); // false true console.log(NaN === NaN, Object.is(NaN, NaN)); // false true4 Predict the output of these type-coercion expressions.
mediumThe plus operator concatenates if either operand is a string, otherwise it adds numbers, converting where needed. The minus, multiply and divide operators always convert to numbers. When an object is converted to a primitive it first tries valueOf and then toString, which is why arrays become comma-joined strings.
javascriptconsole.log(1 + "2", "3" - 1, true + 1); // 12 2 2 console.log([] + [], [] + {}, [1, 2] + [3]); // "" [object Object] 1,23 console.log(null + 1, undefined + 1); // 1 NaN console.log("5" * "2", +"", +"abc"); // 10 0 NaN5 Which values are falsy, and why does it matter?
mediumThe falsy values are false, 0, -0, 0n, the empty string, null, undefined and NaN. Everything else is truthy, including empty arrays, empty objects and the string '0'. This matters for defaults: value || 'x' replaces any falsy value, including a legitimate 0 or empty string, while value ?? 'x' replaces only null and undefined.
javascriptconsole.log(Boolean([]), Boolean({}), Boolean("0"), Boolean(0n)); // true true true false const count = 0; console.log(count || 10, count ?? 10); // 10 06 Explain the output of [1, 2, 3].map(parseInt).
mediummap passes three arguments to the callback: value, index and array. parseInt takes a string and an optional radix, so the index becomes the radix. That gives parseInt(1, 0), which treats radix 0 as 10 and returns 1, parseInt(2, 1), which is NaN because radix 1 is invalid, and parseInt(3, 2), which is NaN because 3 is not a binary digit. Use map(Number) or map(x => parseInt(x, 10)).
javascriptconsole.log([1, 2, 3].map(parseInt)); // [1, NaN, NaN] console.log(["1", "2", "3"].map(Number)); // [1, 2, 3]7 Why is 0.1 + 0.2 not equal to 0.3?
mediumJavaScript numbers are IEEE 754 double-precision floats, and 0.1 and 0.2 cannot be represented exactly in binary, so their sum is slightly above 0.3. Compare with a tolerance such as Math.abs(a - b) < Number.EPSILON, or work in integer units like paise and divide at display time. Use BigInt for integers beyond Number.MAX_SAFE_INTEGER.
javascriptconsole.log(0.1 + 0.2); // 0.30000000000000004 console.log(0.1 + 0.2 === 0.3); // false console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON); // true console.log(9007199254740992 === 9007199254740993); // true8 What is the difference between null and undefined?
mediumundefined means a value has not been assigned: an uninitialised variable, a missing property, a missing argument or a function without return. null is an explicit empty value assigned by the programmer. They are loosely equal but not strictly equal, and typeof gives 'undefined' and 'object'. JSON can hold null but not undefined, which is dropped by JSON.stringify in objects.
9 Are JavaScript values passed by value or by reference?
mediumPrimitives are copied by value. Objects are passed as a copy of a reference, sometimes called call by sharing: the function can mutate the shared object, but reassigning the parameter does not affect the caller's variable.
javascriptfunction f(n, o) { n = 2; // local copy o.x = 2; // mutates shared object o = { x: 99 }; // rebinds local only } let n = 1, o = { x: 1 }; f(n, o); console.log(n, o.x); // 1 210 What is the difference between a shallow and a deep copy in JavaScript?
mediumSpread, Object.assign and Array.slice copy only the top level, so nested objects are still shared. structuredClone, available in modern browsers and Node 17 and later, makes a deep copy of most data, including Dates, Maps and cycles, but throws on functions. JSON.parse(JSON.stringify(x)) is a common hack that loses Dates, undefined, functions and Maps.
javascriptconst a = { n: { v: 1 } }; const s = { ...a }; const d = structuredClone(a); a.n.v = 2; console.log(s.n.v, d.n.v); // 2 111 What do the spread and rest operators do?
easyBoth use three dots. Rest collects remaining items into an array in a parameter list or destructuring pattern. Spread expands an iterable or object into individual items, in calls, array literals and object literals. Later properties override earlier ones in object spread.
javascriptconst sum = (...nums) => nums.reduce((a, b) => a + b, 0); console.log(sum(1, 2, 3)); // 6 console.log(Math.max(...[4, 9, 2])); // 9 console.log({ ...{ a: 1, b: 2 }, b: 3 }); // { a: 1, b: 3 }12 What is the difference between for...in and for...of?
mediumfor...in iterates over enumerable property keys of an object, including inherited ones, and gives string keys, so it is rarely right for arrays. for...of iterates over values of any iterable, such as arrays, strings, Maps and Sets. Use Object.keys or entries for plain objects.
javascriptconst arr = ["a", "b"]; for (const i in arr) console.log(i); // "0" "1" for (const v of arr) console.log(v); // "a" "b"13 Why does [10, 9, 1].sort() give [1, 10, 9]?
hardThe default sort converts elements to strings and compares them by UTF-16 code units, so '10' comes before '9'. Pass a comparator for numbers: (a, b) => a - b. sort mutates the array in place and, since ES2019, is guaranteed stable. toSorted returns a copy in newer runtimes.
javascriptconsole.log([10, 9, 1].sort()); // [1, 10, 9] console.log([10, 9, 1].sort((a, b) => a - b)); // [1, 9, 10]14 What do optional chaining and nullish coalescing do?
mediumOptional chaining, a?.b, a?.[k] and a?.(), stops and returns undefined if the left side is null or undefined instead of throwing a TypeError. Nullish coalescing, a ?? b, returns b only when a is null or undefined, unlike || which also replaces 0, empty string and false.
javascriptconst user = { profile: null }; console.log(user.profile?.name); // undefined console.log(user.settings?.theme ?? "light"); // light console.log(user.profile.name); // TypeError15 What does 'use strict' change?
mediumStrict mode turns silent mistakes into errors: assigning to an undeclared variable throws ReferenceError, writing to a read-only property throws TypeError, duplicate parameter names are disallowed and this is undefined rather than the global object in plain function calls. ES modules and class bodies are strict automatically.
Frequently asked questions
Is JavaScript interpreted or compiled?
Modern engines such as V8 parse the source, compile it to bytecode and then optimise hot code with a just-in-time compiler, so the line between the two is blurred. It is generally described as a dynamically typed, JIT-compiled language.
What is the difference between JavaScript and ECMAScript?
ECMAScript is the standard that defines the language, and JavaScript is its most common implementation. Releases are named ES2015, ES2016 and so on.
Is JavaScript single-threaded?
Your code runs on one main thread with an event loop. Browsers and Node can run extra threads through Web Workers and worker threads, which communicate by messages.
How should I revise before a JavaScript interview?
Work through scope and hoisting, closures, this, prototypes, promises and the event loop with output-prediction practice, then build a small project to talk about.
Related guides
More in Programming Language Interview Questions by Language
- JavaScript Promises and Async/Await Interview Questions
- JavaScript Prototypes and Inheritance Interview Questions
- JavaScript this Keyword Interview Questions
- JVM Internals Interview Questions
- MySQL Interview Questions and Answers
- Nth Highest Salary in SQL: Interview Questions
- PL/SQL Interview Questions and Answers
- PostgreSQL Interview Questions and Answers
- Python Coding Interview Questions with Solutions
- Python Decorators Interview Questions
- Python Exception Handling Interview Questions
- Python File Handling Interview Questions