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