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 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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