Interview Q&A / JavaScript

INTERVIEW Q&A · 71 QUESTIONS

JavaScript interview questions

Closures, the event loop, this, promises and the core language questions every frontend round asks.

  1. What is a closure in JavaScript?

    A closure is a function that remembers the variables from the place where it was created, even after that outer function has finished running.

    BeginnerIn-depth2 min read
  2. What is the difference between == and === in JavaScript?

    === (strict equality) is true only if both values have the same type and the same value. == (loose equality) first converts the values to a common type and then compares, which gives surprising results like 0 == "" being true. Use === by default.

    BeginnerIn-depth3 min read
  3. What is the difference between promises and async/await, and how do you handle errors with each?

    async/await is cleaner syntax built on top of promises. An async function always returns a promise, and await pauses that function until a promise settles. With promises you handle errors using .catch(); with async/await you use try/catch around the await.

    BeginnerIn-depth3 min read
  4. What is the difference between var, let and const? Explain hoisting and the temporal dead zone.

    var is function-scoped and is hoisted with the value undefined. let and const are block-scoped and are hoisted too, but cannot be used before their declaration line; that gap is the temporal dead zone. const also cannot be reassigned, though an object it holds can still be changed.

    BeginnerIn-depth3 min read
  5. Are strings mutable in JavaScript?

    No. Strings are immutable primitives. Methods like toUpperCase, slice and replace never change the original; they return a new string, so you must assign or use the result.

    BeginnerQuick answer1 min read
  6. How do default values and renaming work in object and array destructuring?

    In objects, {name: n = 'x'} reads property name into variable n with a default used only when the value is undefined. In arrays, position matters: [a, , c = 9]. Defaults ignore null.

    BeginnerQuick answer1 min read
  7. How do Number(), parseInt() and the unary plus differ when converting a string to a number?

    Number() and unary + are strict: any junk gives NaN. parseInt reads digits from the left and stops at the first non-digit, so "42px" gives 42. Always pass a radix to parseInt.

    BeginnerQuick answer1 min read
  8. How do you check whether a property exists on an object?

    Use Object.hasOwn(obj, key) for own properties and the in operator to include inherited ones. Comparing to undefined is unreliable, because a property can exist and hold undefined.

    BeginnerQuick answer1 min read
  9. How do you check whether a value is an array in JavaScript?

    Use Array.isArray(value). typeof returns "object" for arrays, and instanceof Array can fail for arrays created in another frame or realm.

    BeginnerQuick answer1 min read
  10. How do you remove duplicate values from an array?

    The shortest way is [...new Set(arr)], because a Set stores each value once and keeps insertion order. Filtering with indexOf also works but is slower. For objects, dedupe by a key using a Map.

    BeginnerQuick answerCoding1 min read
  11. How does Array.reduce work, and why should you give it an initial value?

    reduce walks the array carrying an accumulator: each callback call gets the accumulator and the current item and returns the next accumulator. Without an initial value, an empty array throws a TypeError.

    BeginnerQuick answer1 min read
  12. How would you mask all but the last four digits of a card number in JavaScript?

    Take the last four characters with slice(-4) and pad the front back to the original length with padStart(length, '*'). padStart and padEnd fill a string to a target length from the left or right.

    BeginnerQuick answer1 min read
  13. Is JavaScript pass by value or pass by reference?

    JavaScript is always pass by value. For objects, the value passed is a reference, so a function can change the object's contents but cannot make the caller's variable point to a different object.

    BeginnerQuick answer1 min read
  14. What are template literals and why are they better than string concatenation?

    Template literals use backticks. They allow ${expression} interpolation and multi-line strings without escape characters, which makes strings with variables far easier to read than chains of + signs.

    BeginnerQuick answer1 min read
  15. What are the primitive data types in JavaScript?

    JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null and symbol. Everything else, including arrays and functions, is an object.

    BeginnerQuick answer1 min read
  16. What are the states of a Promise, and what happens if you resolve it twice?

    A promise is pending, then becomes fulfilled with a value or rejected with a reason. Once settled it never changes, so later resolve or reject calls are ignored.

    BeginnerQuick answer1 min read
  17. What are truthy and falsy values in JavaScript?

    When a value is used in a condition, JavaScript converts it to a boolean. Only eight values become false: false, 0, -0, 0n, empty string, null, undefined and NaN. Everything else, including [] and {}, is truthy.

    BeginnerQuick answer1 min read
  18. What do Object.keys, Object.values, Object.entries and Object.fromEntries do?

    keys gives an array of own enumerable property names, values gives their values, and entries gives [key, value] pairs. fromEntries does the reverse and builds an object from pairs, which makes transforming objects easy.

    BeginnerQuick answer1 min read
  19. What does an async function return?

    Always a Promise. A returned value becomes the fulfilled value, and a thrown error becomes a rejection. That is true even when the function contains no await.

    BeginnerQuick answer1 min read
  20. What does typeof return for a variable that was never declared?

    typeof returns the string "undefined" for an undeclared name and does not throw. Reading the same name any other way throws a ReferenceError.

    BeginnerQuick answer1 min read
  21. What is an IIFE and why would you use one?

    An IIFE (immediately invoked function expression) is a function that is defined and called in one go, like (function () { ... })(). It creates a private scope so variables inside do not leak out.

    BeginnerQuick answer1 min read
  22. What is lexical scope in JavaScript?

    Lexical scope means a function's accessible variables are decided by where it is written in the source code, not by where it is called. Inner code can see outer variables, never the reverse.

    BeginnerQuick answer1 min read
  23. What is NaN, and how do you check whether a value is NaN?

    NaN means 'Not a Number' and comes from failed numeric operations like Number("abc"). It is the only value not equal to itself, so use Number.isNaN(value), which avoids the coercion done by the older global isNaN.

    BeginnerQuick answer1 min read
  24. What is the difference between a function declaration and a function expression?

    A declaration is hoisted with its body, so you can call it before the line it appears on. An expression assigns a function to a variable, and the variable follows var, let or const rules, so calling it early fails.

    BeginnerQuick answer1 min read
  25. What is the difference between a shallow copy and a deep copy of an object?

    A shallow copy duplicates only the top level, so nested objects are still shared with the original. A deep copy duplicates every level, so the two are fully independent. Spread and Object.assign are shallow; structuredClone is deep.

    BeginnerQuick answer1 min read
  26. What is the difference between call, apply and bind?

    All three set this for a function. call runs it now with arguments listed one by one, apply runs it now with an array of arguments, and bind returns a new function with this fixed, to be run later.

    BeginnerQuick answer1 min read
  27. What is the difference between implicit and explicit type coercion?

    Explicit coercion is when you convert on purpose, like Number("5") or String(5). Implicit coercion is when JavaScript converts for you during an operation, like "5" * 2 or if (value).

    BeginnerQuick answer1 min read
  28. What is the difference between includes and indexOf for arrays?

    includes returns true or false and can find NaN. indexOf returns the position or -1, uses strict equality and can never find NaN. Use includes for a yes/no check and indexOf when you need the position.

    BeginnerQuick answer1 min read
  29. What is the difference between map and forEach?

    map returns a new array built from your callback's return values. forEach returns undefined and is only for side effects like logging. Use map to transform data and forEach to do something with each item.

    BeginnerQuick answer1 min read
  30. What is the difference between null and undefined?

    undefined means a value was never assigned: JavaScript gives it to you automatically. null means 'no value' and is set on purpose by the programmer. They are loosely equal (null == undefined) but not strictly equal.

    BeginnerQuick answer1 min read