Recursion — JS Technical Interview Questions & Answers

A function that calls itself, with a base case to stop.

Recursion

  • Recursion (js) is a JS topic. In plain words you use it for a short Recursion js example in a script in the browser. Don’t start with a slogan — start with that picture.
  • Smallest example: the smallest Recursion js example you can type. Type it, run it, and say what you see. If you can do that from memory, you know Recursion (js).
  • From the example next to this theory: A function that calls itself, with a base case to stop.
  • Trap: only saying “Recursion js” with no example. Fix that before you talk about advanced JS.
  • Viva: what is Recursion (js)? Then show the smallest Recursion js example you can type. Then name the trap.

Technical Interview Tip: What is Recursion (js)? Show this: the smallest Recursion js example you can type. Trap: only saying “Recursion js” with no example.

Code Examples for Recursion

Recursion — working example

function greet(name = "friend") {
  return "Hi " + name;
}
const shout = function (msg) {
  return msg.toUpperCase();
};
(function () {
  console.log("IIFE ran");
})();

function mapNums(nums, fn) {
  const out = [];
  for (const n of nums) out.push(fn(n));
  return out;
}
console.log(greet("Ada"), shout("js"));
console.log(mapNums([1, 2], (n) => n * 2));

function factorial(n) {
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}
console.log(factorial(4));

Recursion — functions take parameters, can be expressions/IIFEs/callbacks, and recurse with a base case. Pure functions avoid side effects.

How to Explain Recursion to an Interviewer

What is Recursion (js)?

Used for a short Recursion js example a script in the browser Not just the heading

Give one small example of Recursion (js).

the smallest Recursion js example you can type A function that calls itself, with a base case to stop

What mistake do beginners make with Recursion (js)?

only saying “Recursion js” with no example Show the fix

Where do you use Recursion (js)?

a script in the browser a short Recursion js example