Interview Q&A / JavaScript
JavaScript interview questions
Closures, the event loop, this, promises and the core language questions every frontend round asks.
Prefer rapid revision? Practise JavaScript short interview answers →
- 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 - 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 - 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 - 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