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 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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