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 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
  2. 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
  3. 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
  4. 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
  5. What is the difference between replace and replaceAll on strings?

    replace with a string pattern changes only the first match. replaceAll changes every match. With a regular expression, replace needs the g flag to change all matches, and replaceAll throws if the regex lacks it.

    BeginnerQuick answer1 min read
  6. What is the difference between slice and splice?

    slice copies a section into a new array and leaves the original alone. splice changes the original array in place by removing, replacing or inserting items, and returns the removed items.

    BeginnerQuick answer1 min read
  7. What is the difference between slice and substring on a string?

    Both return part of a string without changing it. slice accepts negative indexes, counting from the end, and returns an empty string if start is after end. substring treats negatives as 0 and swaps its arguments if start is greater than end.

    BeginnerQuick answer1 min read
  8. What is the difference between the spread and rest operators?

    Both are written as three dots. Rest collects many values into one array or object, in function parameters and destructuring. Spread expands one array or object into many items, in calls and literals.

    BeginnerQuick answer1 min read
  9. What is the scope chain and how does JavaScript look up a variable?

    The scope chain is the list of scopes from the current one outward to global. When you use a name, the engine checks the innermost scope first and moves outward until it finds it, or throws a ReferenceError.

    BeginnerQuick answer1 min read
  10. When would you use filter, find, some and every on an array?

    filter returns all matches as an array, find returns the first match or undefined, some says whether at least one matches, and every says whether all match. some and every stop as soon as the answer is known.

    BeginnerQuick answer1 min read
  11. Why does "5" + 3 give "53" but "5" - 3 give 2?

    The + operator does string concatenation if either side is a string, so 3 becomes "3". The -, * and / operators only work on numbers, so they convert "5" to 5 first.

    BeginnerQuick answer1 min read
  12. Why does [10, 9, 1].sort() give [1, 10, 9], and how do you sort numbers correctly?

    By default sort converts items to strings and compares them in dictionary order, so "10" comes before "9". Pass a comparator like (a, b) => a - b to sort numbers. Note that sort changes the original array.

    BeginnerQuick answer1 min read
  13. Why does typeof null return "object" in JavaScript?

    It is a bug from the first version of JavaScript: values carried a type tag, and null used the same tag as objects. Fixing it would break old websites, so it stays. Check for null with value === null.

    BeginnerQuick answer1 min read
  14. Write a function once(fn) that lets fn run only the first time it is called.

    Return a wrapper that keeps a called flag and the first result in a closure. On the first call, run fn and store the result; on later calls, return the stored result without running fn.

    BeginnerQuick answerCoding1 min read
  15. Write a function that returns the first non-repeating character in a string.

    Count how many times each character appears using a Map in one pass, then scan the string a second time and return the first character whose count is 1. Return null if none exists.

    BeginnerQuick answerCoding1 min read
  16. Write a function to reverse a string and use it to check for a palindrome.

    Split the string into characters, reverse the array and join it back. A palindrome reads the same both ways, so clean the text (lowercase, letters and digits only) and compare it with its reverse.

    BeginnerQuick answerCoding1 min read
  17. How can awaiting inside a loop make code slow, and how do you run async tasks in parallel?

    Each await pauses until that task finishes, so awaiting one after another runs them in sequence. Start all the promises first and await them together with Promise.all to run them in parallel.

    IntermediateQuick answer1 min read
  18. How can you create private variables in JavaScript using closures?

    Declare the variable inside a function and return only functions that use it. The variable lives on in the closure, but no outside code can read or change it directly.

    IntermediateQuick answer1 min read
  19. How do you handle errors with async/await, and why can try/catch miss some of them?

    Wrap await calls in try/catch. The catch only sees rejections that are awaited inside the try. A promise you forget to await, or return without awaiting, escapes the block.

    IntermediateQuick answer1 min read
  20. How does promise chaining work, and what happens after a catch?

    Every then returns a new promise, resolved with whatever the handler returns, so calls chain. A thrown error skips later then handlers until a catch, and after a catch that returns a value, the chain continues normally.

    IntermediateQuick answer1 min read
  21. How does this behave inside an arrow function?

    An arrow function has no this of its own. It uses the this of the surrounding code where it was written, and call, apply and bind cannot change it.

    IntermediateQuick answer1 min read
  22. How is Object.is different from the === operator?

    Object.is works like === with two differences: Object.is(NaN, NaN) is true, and Object.is(0, -0) is false. React uses it to compare state values.

    IntermediateQuick answer1 min read
  23. What do [] + [] and [] + {} evaluate to in JavaScript?

    [] + [] is the empty string and [] + {} is "[object Object]". The + operator turns both sides into strings when neither is a number, and an empty array stringifies to an empty string.

    IntermediateQuick answer1 min read
  24. What is BigInt in JavaScript and when would you use it?

    BigInt is a primitive for whole numbers of any size, written with an n suffix like 123n. Use it when values pass Number.MAX_SAFE_INTEGER (2^53 - 1), such as large IDs. It cannot be mixed with regular numbers.

    IntermediateQuick answer1 min read
  25. What is method borrowing, and how do call and apply let you do it?

    Method borrowing means using a method from one object on another by passing the other as this with call or apply. A classic case is Array.prototype.slice.call(arguments) on an array-like object.

    IntermediateQuick answer1 min read
  26. What is the difference between Object.freeze, Object.seal and Object.preventExtensions?

    preventExtensions blocks adding properties. seal also blocks deleting and reconfiguring, but allows changing values. freeze blocks everything, so values are read-only too. All three are shallow.

    IntermediateQuick answer1 min read
  27. What is the difference between Promise.all, allSettled, race and any?

    all waits for every promise and rejects on the first failure. allSettled waits for all and reports each outcome. race settles with the first promise to settle. any fulfills with the first success and rejects only if all fail.

    IntermediateQuick answer1 min read
  28. What is this inside a plain function call in strict mode versus sloppy mode?

    In sloppy mode a plain call gets the global object as this. In strict mode it gets undefined. Strict mode also stops call from boxing a primitive this into an object.

    IntermediateQuick answer1 min read
  29. Which array methods change the original array and which return a new one?

    push, pop, shift, unshift, splice, sort, reverse and fill change the original. map, filter, slice, concat, flat and the newer toSorted, toReversed and toSpliced return new arrays.

    IntermediateQuick answer1 min read
  30. Why does ['1', '2', '3'].map(parseInt) not give [1, 2, 3]?

    map passes three arguments to the callback: item, index and array. parseInt uses the second as the radix, so it runs parseInt('2', 1) and parseInt('3', 2), both NaN. Use map(Number) or map(s => parseInt(s, 10)).

    IntermediateQuick answer1 min read