Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 77 ยท JavaScript Interview Questions

JavaScript: Arrays, Objects and Functional Patterns

Prepare for JavaScript interviews on map, filter, reduce, immutability, shallow vs deep copy, debounce, throttle and currying, with runnable examples.

Array methods and functional patterns appear in almost every JavaScript interview, either as direct questions ("implement reduce") or hidden inside a bigger task ("group these orders by customer"). Interviewers look for three things: that you pick the right method, that you avoid accidental mutation, and that you can write the small helpers (debounce, curry) from scratch.

You can find the long-form teaching in array methods, higher-order functions, copying objects and functional programming. This lesson is the interview version.

map, filter and reduce

Three workhorses

map One output per input. Same length.
filter Keep items that pass a test.
reduce Fold everything into one value.

None of them changes the original array.

1 / 2

map transforms, filter selects, reduce accumulates. Everything else (some, every, find, flatMap) is a variation. A strong answer also says what each returns: new array, new array, and any single value (a number, an object, even another array).

JS Group, count and total with reduce

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Two classic gotchas to mention: always pass an initial value to reduce, and ["1", "2", "3"].map(parseInt) gives [1, NaN, NaN] because map passes the index as the second argument, which parseInt reads as the radix.

Mutation and immutability

Some methods change the array in place (push, pop, splice, sort, reverse), others return a new one (map, filter, slice, concat, and the newer toSorted, toReversed, toSpliced, with). Interviewers ask about it because mutated shared state causes hard bugs, and because frameworks like React compare references to detect change.

JS Mutating versus non-mutating

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Object.freeze is shallow: nested objects stay writable. This leads naturally to the next question.

Shallow versus deep copy

A shallow copy duplicates the top level only. Nested objects are still shared between the original and the copy. A deep copy duplicates everything down the tree. Spread ({ ...obj }), Object.assign, Array.from and slice() are all shallow.

flowchart LR
  A["original.address"] --> C["address object"]
  B["shallowCopy.address"] --> C

After a shallow copy, both objects point to the same nested object.

JS Shallow copy surprise and the deep options

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In an answer, recommend structuredClone for plain data, mention that the JSON trick drops undefined, functions, and turns dates into strings, and note that you can often avoid deep copies by updating only the parts that change.

debounce and throttle

Both limit how often a function runs while events fire rapidly. Debounce waits until events stop for a given time, then runs once (search-as-you-type). Throttle runs at most once per interval while events keep coming (scroll handlers).

JS debounce and throttle from scratch

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Both rely on closures to keep timer or last between calls. Mention the details interviewers probe: preserving this and arguments, and (for debounce) a "leading edge" option that runs immediately on the first call.

Currying and composition

Currying turns a function of several arguments into a chain of functions that each take one (or some). It enables partial application: fix the first arguments now, supply the rest later.

JS A generic curry and compose

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The limit shown at the end is a typical follow-up: curry depends on fn.length, which does not count default or rest parameters.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does [1, 2, 3].map(parseInt) return?

    map passes (value, index), so parseInt('2', 1) and parseInt('3', 2) get an invalid radix or digit and return NaN.
  2. 2. Which of these is a deep copy?

    Spread, Object.assign and slice copy only the top level. structuredClone copies nested data too.
  3. 3. Which description matches debounce?

    Debounce resets a timer on every event and runs the function only after a quiet period. Throttle is the one that limits the rate.
  4. 4. Why is passing an initial value to reduce recommended?

    With no initial value and no elements, there is nothing to start from, so reduce throws.

Summary

  • map transforms, filter selects, reduce folds; give reduce an initial value.
  • Know which methods mutate; prefer toSorted, spread and other non-mutating forms.
  • Spread and Object.assign copy shallowly; use structuredClone for deep copies of plain data.
  • Debounce waits for quiet, throttle limits the rate; both use closures.
  • Currying and composition build functions from smaller functions.

Next: Classic Coding Questions.