Functions, Iterables and Collections
JavaScript treats functions as ordinary values and ships a small set of built-in collections — arrays, Map/Set, and anything iterable — that you are expected to wield fluently. Interviewers lean on this area because almost every coding round is really a question about picking the right collection and expressing the transformation with the right method.
part ofJavaScriptoverview, primer and where to startread it →on this pageshowhide
explore
- Functions as Values17 questions
- Higher-Order Functions and Callbacks6 questions
- Currying and Partial Application5 questions
- Parameters, Defaults and Rest6 questions
- Arrays and Array Methods26 questions
- map, filter, reduce and flatMap5 questions
- Searching and Predicate Methods5 questions
- Sorting and Comparator Pitfalls6 questions
- Copying vs Mutating Methods5 questions
- Destructuring and Spread5 questions
- Keyed Collections13 questions
- Map vs Plain Object4 questions
- Set Semantics and Uniqueness4 questions
- WeakMap, WeakSet and GC5 questions
- Iterators and Generators19 questions
- Iteration Protocols and Symbol.iterator5 questions
- for...of vs for...in vs forEach5 questions
- Generator Functions and yield4 questions
- yield* Delegation and Lazy Sequences5 questions
questions
75 · 4 sectionsIn JavaScript, what makes a function a higher-order function, and which property of the language makes higher-order functions possible?
basics
~20 sA higher-order function takes a function as an argument, returns a function, or both. It works because JavaScript functions are first-class values: they can be stored in variables, put in arrays or objects, passed as arguments, and returned like any other value.
In JavaScript, when does a default parameter value actually apply, and what happens if the caller passes null?
basics
~20 sA default applies only when the argument is undefined — omitted, or explicitly passed as undefined. Every other value, including null, 0, empty string, false and NaN, is used as-is, so null never triggers the default.
What does `['1', '7', '11'].map(parseInt)` return, and what does that result reveal about how a higher-order function calls the callback you hand it?
basics
~20 sIt returns [1, NaN, 3]. The callback is invoked with three arguments — element, index and array — so parseInt receives the index as its radix: radix 1 is invalid and radix 2 reads "11" as binary 3. Wrap the callback to control its arity.
In JavaScript, what does a ...rest parameter give you that the legacy arguments object does not?
basics
~20 sA rest parameter binds a real Array, so map, filter and reduce work directly, and it holds only the arguments beyond the named ones. The arguments object is array-like, holds every argument, is not available in arrow functions, and needs conversion before array methods work.
How does `Function.prototype.bind` let you partially apply a JavaScript function, and what happens to arguments passed later to the bound function?
basics
~20 sEvery argument after bind's first is stored and prepended to each later call, so greet.bind(null, 'Hi') returns a function that always supplies 'Hi' first and appends whatever the caller passes. Only leading arguments can be pinned this way.
In JavaScript, what is the difference between Array.prototype.slice() and Array.prototype.splice(), and what does each one return?
basics
~20 sslice() copies: it leaves the original array untouched and returns a new array holding the selected range. splice() edits in place: it removes and/or inserts elements in the original array and returns an array of the elements it removed.
In JavaScript array destructuring, when does a default value like `const [a = 5] = values` actually apply — and what do you get when the matching element is null, a hole, or missing entirely?
basics
~20 sA destructuring default fires only when the matched value is exactly undefined — a missing index, an array hole, or an explicit undefined. null, 0, empty string and NaN are real values, so they are bound as-is.
In JavaScript, what do Array.prototype.find and Array.prototype.findIndex return when no element satisfies the predicate, and what bug does that difference commonly cause?
basics
~20 sArray.prototype.find returns undefined when nothing matches, while findIndex returns -1. The bug is truthiness: -1 is truthy and index 0 is falsy, so testing findIndex's result directly inverts the check. Compare it against -1 instead.
Why does [10, 9, 1].sort() return [1, 10, 9] in JavaScript, and how do you sort an array of numbers correctly?
basics
~20 sArray.prototype.sort called with no comparator converts every element to a string and compares those strings, so "10" sorts before "9". Pass a numeric comparator, arr.sort((a, b) => a - b), to order numbers by value.
In JavaScript, what is the difference between Array.prototype.map and Array.prototype.filter, and what does each one return?
basics
~20 sArray.prototype.map builds a new array of the same length by replacing each element with whatever its callback returns. Array.prototype.filter builds a new array holding only the elements whose callback returned a truthy value. Neither changes the original array.
What does this JavaScript log, and why: `const o = {}; o[1] = 'a'; o['1'] = 'b'; const m = new Map(); m.set(1, 'a'); m.set('1', 'b'); console.log(Object.keys(o).length, m.size);`
basics
~20 sIt logs 1 and 2. Plain-object property keys are always strings or symbols, so o[1] and o['1'] are the same property. Map stores keys exactly as given and compares them without coercion, so the number and the string are separate entries.
In JavaScript, what does the expression [...new Set(myArray)] produce, and what are the limits of it as a deduplication idiom?
basics
~20 sSpreading an array into a Set and back yields a new array with duplicates removed and first-occurrence order preserved, because a Set stores each value only once. It collapses only values the Set treats as equal; distinct objects with identical contents survive.
In JavaScript, how does a WeakMap differ from a Map, and what values are allowed as WeakMap keys?
basics
~20 sA WeakMap holds its keys weakly, so an entry becomes garbage-collectable as soon as nothing else references the key object. Keys must be objects (or unregistered symbols), and the API is only get, set, has and delete — no size, no iteration.
When would you choose a JavaScript Map over a plain object for a key-value store, and when is the plain object still the better choice?
basics
~20 sUse a Map when keys are dynamic, non-string, or arbitrary user input, and when you need insertion-order iteration or a running count. Prefer a plain object for fixed, known string fields and for data that must serialize to JSON.
What equality rule does a JavaScript Set use to decide whether a value is already present, and what surprising results does it produce for NaN, -0, and objects?
basics
~20 sA Set uses SameValueZero: like === except NaN counts as equal to itself, and +0 equals -0. So NaN deduplicates, 0 and -0 collapse into one entry stored as +0, and objects are matched by reference, never by their contents.
Given `const arr = ['a', 'b', 'c']`, what does `for (const x in arr)` give you on each pass compared with `for (const x of arr)`, and why is for...in the wrong loop for arrays?
basics
~20 sfor...in yields property keys as strings — "0", "1", "2" — plus any other enumerable property on the array or its prototype chain. for...of yields the values "a", "b", "c". Arrays hold values, so for...of is the right loop.
In JavaScript, what happens when you call a generator function declared with `function*` — does its body run, and what do you get back?
basics
~20 sCalling a generator function runs none of its body. It returns a generator object — a paused iterator. The body advances only when you call next(), running until the next yield and then suspending with its local state intact.
In JavaScript, what makes a value iterable, and what must the object returned by its Symbol.iterator method look like?
basics
~20 sA value is iterable if it has a Symbol.iterator method that returns an iterator — an object whose next() method returns { value, done }. Arrays, strings, Map and Set have one; plain objects do not.
In JavaScript, you need the first five results of mapping and filtering an unbounded sequence of numbers. Why do chained array methods fail here, and how does a generator pipeline solve it?
basics
~20 sArray map and filter are eager: they need a finished array and build a new one at every stage, so an unbounded source never gets past the first call. Generators are pull-based, so a take stage requests only the five values it needs.
Inside an `Array.prototype.forEach` callback, what do `return` and `break` actually do, and how do you stop iterating early?
basics
~20 sreturn inside a forEach callback only ends that one callback invocation, behaving like continue; break is a SyntaxError because there is no enclosing loop. forEach cannot be stopped early — use for...of with break, or a short-circuiting method like some or find.