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 check whether a value is an array in JavaScript?
Use Array.isArray(value). typeof returns "object" for arrays, and instanceof Array can fail for arrays created in another frame or realm.
BeginnerQuick answer1 min read - 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 - How does Array.reduce work, and why should you give it an initial value?
reduce walks the array carrying an accumulator: each callback call gets the accumulator and the current item and returns the next accumulator. Without an initial value, an empty array throws a TypeError.
BeginnerQuick answer1 min read - 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 - 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 - 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 - 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 - 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