Immediately Invoked Function Expressions (IIFE) — JS Technical Interview Questions & Answers

A function that runs as soon as it's defined.

Immediately Invoked Function Expressions (IIFE)

  • Immediately Invoked Function Expressions (IIFE) (js) is a JS topic. In plain words you use it for add(a, b) in a checkout total. Don’t start with a slogan — start with that picture.
  • Smallest example: function add / const add = () =>. Type it, run it, and say what you see. If you can do that from memory, you know Immediately Invoked Function Expressions (IIFE) (js).
  • From the example next to this theory: A function that runs as soon as it's defined.
  • Trap: this surprise in arrow vs function. Fix that before you talk about advanced JS.
  • Viva: what is Immediately Invoked Function Expressions (IIFE) (js)? Then show function add / const add = () =>. Then name the trap.

Technical Interview Tip: What is Immediately Invoked Function Expressions (IIFE) (js)? Show this: function add / const add = () =>. Trap: this surprise in arrow vs function.

Code Examples for Immediately Invoked Function Expressions (IIFE)

Immediately Invoked Function Expressions (IIFE) — 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));

Immediately Invoked Function Expressions (IIFE) — functions take parameters, can be expressions/IIFEs/callbacks, and recurse with a base case. Pure functions avoid side effects.

How to Explain Immediately Invoked Function Expressions (IIFE) to an Interviewer

What is Immediately Invoked Function Expressions (IIFE) (js)?

Used for add(a, b) a checkout total Not just the heading

Give one small example of Immediately Invoked Function Expressions (IIFE) (js).

function add / const add = () => A function that runs as soon as it's defined

What mistake do beginners make with Immediately Invoked Function Expressions (IIFE) (js)?

this surprise in arrow vs function Show the fix

Where do you use Immediately Invoked Function Expressions (IIFE) (js)?

a checkout total add(a, b)