skip to content

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 pageshow

explore

questions

75 · 4 sections

In JavaScript, what makes a function a higher-order function, and which property of the language makes higher-order functions possible?

level: juniorimportance: must knowfreq 82%
basics
~20 s

A 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.

open as a page

In JavaScript, when does a default parameter value actually apply, and what happens if the caller passes null?

level: juniorimportance: must knowfreq 75%
basics
~20 s

A 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.

open as a page

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?

level: middleimportance: must knowfreq 60%
basics
~20 s

It 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.

open as a page

In JavaScript, what does a ...rest parameter give you that the legacy arguments object does not?

level: middleimportance: must knowfreq 70%
basics
~20 s

A 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.

open as a page

How does `Function.prototype.bind` let you partially apply a JavaScript function, and what happens to arguments passed later to the bound function?

level: juniorimportance: should knowfreq 42%
basics
~20 s

Every 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.

open as a page

In JavaScript, what is the difference between Array.prototype.slice() and Array.prototype.splice(), and what does each one return?

level: juniorimportance: must knowfreq 80%
basics
~20 s

slice() 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.

open as a page

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?

level: juniorimportance: must knowfreq 72%
basics
~20 s

A 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.

open as a page

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?

level: juniorimportance: must knowfreq 72%
basics
~20 s

Array.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.

open as a page

Why does [10, 9, 1].sort() return [1, 10, 9] in JavaScript, and how do you sort an array of numbers correctly?

level: juniorimportance: must knowfreq 85%
basics
~20 s

Array.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.

open as a page

In JavaScript, what is the difference between Array.prototype.map and Array.prototype.filter, and what does each one return?

level: juniorimportance: must knowfreq 82%
basics
~20 s

Array.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.

open as a page

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);`

level: juniorimportance: must knowfreq 58%
basics
~20 s

It 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.

open as a page

In JavaScript, what does the expression [...new Set(myArray)] produce, and what are the limits of it as a deduplication idiom?

level: juniorimportance: must knowfreq 72%
basics
~20 s

Spreading 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.

open as a page

In JavaScript, how does a WeakMap differ from a Map, and what values are allowed as WeakMap keys?

level: juniorimportance: must knowfreq 68%
basics
~20 s

A 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.

open as a page

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?

level: middleimportance: must knowfreq 78%
basics
~20 s

Use 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.

open as a page

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?

level: middleimportance: must knowfreq 60%
basics
~20 s

A 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.

open as a page

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?

level: juniorimportance: must knowfreq 78%
basics
~20 s

for...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.

open as a page

In JavaScript, what happens when you call a generator function declared with `function*` — does its body run, and what do you get back?

level: juniorimportance: must knowfreq 55%
basics
~20 s

Calling 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.

open as a page

In JavaScript, what makes a value iterable, and what must the object returned by its Symbol.iterator method look like?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A 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.

open as a page

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?

level: middleimportance: must knowfreq 50%
basics
~20 s

Array 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.

open as a page

Inside an `Array.prototype.forEach` callback, what do `return` and `break` actually do, and how do you stop iterating early?

level: middleimportance: must knowfreq 68%
basics
~20 s

return 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.

open as a page