02 / 04

What are first class functions?

Difficulty: 3/10
higher-order functions, callbacks, closures

A programming language is said to have first class functions if functions are treated as any other variable and can be passed as arguments, returned from a function or can be assigned to a variable as value.

Scenario Questions

0-2 years experience

  1. 1We need to filter an array of numbers to keep only the even ones. How would you write that using a function passed to `Array.prototype.filter`, and why does that work in JavaScript?
  2. 2If you assign a function `function add(a, b) { return a + b; }` to a variable like `const sum = add;`, what can you do with `sum` that shows functions are first‑class?
  3. 3Show a short example of using `setTimeout` with an inline function. What does passing a function to `setTimeout` illustrate about first‑class functions?

2-5 years experience

  1. 1Our utility `mapValues(obj, fn)` started throwing because `fn` sometimes receives the wrong `this` context. Explain how treating functions as first‑class values can lead to this and how you’d fix it.
  2. 2We refactored a module to accept a logging callback, but callers passing arrow functions see `undefined` errors. Walk me through why that happens with first‑class functions and lexical `this`.
  3. 3Design a debounce helper that returns a new function wrapping the original. How does the fact that functions are first‑class enable this pattern, and what edge cases should you guard against?

5-8 years experience

  1. 1Our event system lets plugins register handlers via `register(eventName, handler)`. At scale we notice memory leaks because handlers aren’t released. Discuss how treating functions as first‑class objects affects garbage collection and what design changes you’d propose.
  2. 2We want to let users supply custom validation logic that can be composed with built‑in validators. Sketch an API that leverages first‑class functions for composition while keeping performance acceptable.
  3. 3A legacy codebase stores callbacks in a plain array and later invokes them with `call`. Occasionally a callback throws and crashes the process. Propose a more robust execution strategy using first‑class functions to isolate failures.

8+ years experience

  1. 1Our platform is moving from a callback‑heavy architecture to a declarative pipeline built on higher‑order functions. What architectural considerations—API surface, backward compatibility, team onboarding—arise when you promote functions to first‑class citizens at scale?
  2. 2We plan to expose a shared plugin system where plugins are JavaScript functions executed in a sandbox across multiple microservices written in different languages. Explain the security, serialization, and versioning challenges of treating functions as first‑class assets across service boundaries.
  3. 3When migrating a large codebase to treat functions as first‑class values for extensibility, how would you balance the need for runtime flexibility with compile‑time safety and long‑term maintainability?

Follow-up Questions

  • Can you walk me through a concrete code snippet that demonstrates this?
  • What pitfalls arise when you pass functions around, especially regarding `this`?
  • How would you unit‑test a function that receives another function as a parameter?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.