Facebook Pixel

Functional Programming Interview Questions in JavaScript

FP is a common interview topic. Here are the most asked questions with answers and code.

Functional Programming Interview Questions in JavaScript

Functional programming is a common interview topic. Here are the most asked questions with answers and code.

Q1: What is a pure function?

A function that given the same input always returns the same output and has no side effects. It does not modify external state or read mutable external state.

Q2: What is immutability in JavaScript?

Not mutating data. Instead of changing an object, you create a new copy with the changes. Use spread: { ...obj, x: 2 } or [...arr, 4].

Q3: Implement a compose function.

function compose(...fns) { return (x) => fns.reduceRight((acc, fn) => fn(acc), x); } `` ### Q4: Implement a curry function. ```js function curry(fn) { return function curried(...args) { if (args.length >= fn.length) return fn(...args); return (...next) => curried(...args, ...next); }; } `` ### Q5: Implement a memoize function. ```js function memoize(fn) { const cache = {}; return (...args) => { const key = JSON.stringify(args); if (!(key in cache)) cache[key] = fn(...args); return cache[key]; }; } `` ### Q6: What is the difference between map and forEach? `map` returns a new array (use for transformation). `forEach` returns `undefined` (use for side effects). Do not use `map` if you do not need the return value. ### Q7: What is a higher-order function? A function that takes a function as an argument, returns a function, or both. Examples: `map`, `filter`, `reduce`, `compose`, `curry`, `setTimeout`. ### Q8: What is the difference between declarative and imperative code? Imperative describes how (loops, mutations). Declarative describes what (`map`, `filter`, `reduce`). FP is declarative. ### Q9: What is function composition? Chaining functions so each function's output is the next function's input. `compose(f, g)(x) = f(g(x))`. It builds complex behavior from simple functions. ### Q10: What is a side effect in JavaScript? Anything that interacts with the outside world: modifying external state, I/O, network calls, DOM manipulation, console.log. Pure functions have no side effects. ### The Takeaway FP interview questions test: pure functions, immutability, `compose`/`curry`/`memoize` implementations, `map` vs `forEach`, HOF definition, declarative vs imperative, function composition, and side effects. Practice implementing each utility from scratch.

A function that given the same input always returns the same output and has no side effects. It does not modify external state or read mutable external state. Pure functions are easy to test and reason about.

Return a function that applies the given functions right to left using reduceRight: return (x) => fns.reduceRight((acc, fn) => fn(acc), x). compose(f, g)(x) = f(g(x)).

map returns a new array (use for transformation). forEach returns undefined (use for side effects). Do not use map if you do not need the return value; it wastes memory.

Anything that interacts with the outside world: modifying external state, I/O, network calls, DOM manipulation, console.log, Math.random, Date.now. Pure functions have no side effects.

Chaining functions so each function's output is the next function's input. compose(f, g)(x) = f(g(x)). It builds complex behavior from simple, reusable functions. pipe is the same but left to right.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.