JavaScriptClosures And Functionsintermediate

JavaScript Closures and Functions Interview Questions

Closures, this and function patterns are where JavaScript interviews get serious — here are the eight questions that come up most, with answers and code you can write on a whiteboard.

Published
TL;DR – Quick Answer

JavaScript function interviews centre on closures and the this keyword, then call, apply and bind, function declarations versus expressions, IIFEs, currying, higher-order and pure functions, and memoization. Interviewers want a short definition, a working example and one real use, such as keeping data private with a closure.

On This Page

Functions are where JavaScript is most different from other languages, and interviewers spend real time here. This guide covers closures, how this is decided, call/apply/bind, declarations versus expressions, IIFEs, currying, higher-order functions and memoization — the eight questions that separate people who use JavaScript from people who understand it.

Why interviewers ask about JavaScript closures and functions

Closures and this explain most of the confusing behaviour in JavaScript: callbacks that print the wrong value, methods that lose their object, event handlers that see stale data. An interviewer who asks about them is really asking whether you can debug those problems.

These questions also lead naturally into coding tasks — write a counter with private state, implement bind, build a memoize helper — so expect to write code, not just talk.

How to answer these questions

For closures, always show a small example and one real use: data privacy, a function factory, or the classic var-in-a-loop bug. For this, list the call-site rules in order — new, explicit binding, method call, plain call, arrow — and apply them to the snippet in front of you.

If ES6 syntax still slows you down, revise ES6 features first, because most of these answers use arrow functions and rest parameters.

Q1. What is a closure in JavaScript?

A closure is a function that keeps access to the variables of the scope where it was created, even after that outer function has returned.

function counter() {
  let count = 0;
  return function () {
    count++;
    return count;
  };
}
const inc = counter();
inc(); // 1
inc(); // 2  ← count persists inside the closure

Closures power module patterns, memoization and data encapsulation.

Interview note: Classic trap: for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)) prints 3 3 3, because every callback closes over the same var i. Using let creates a new binding per iteration and prints 0 1 2.

Q2. How is the value of this decided in JavaScript?

this is decided by how a function is called, not where it is written. A method call uses the object before the dot, a plain call gives undefined in strict mode (the global object otherwise), new gives the new object, call/apply/bind set it explicitly, and arrow functions take this from the surrounding scope.

const user = {
  name: "Ravi",
  hi() { return this.name; },
};
user.hi();                  // "Ravi" — called on user

const fn = user.hi;
fn();                       // TypeError in strict mode — this is undefined, so this.name fails
fn.call({ name: "Siva" });  // "Siva" — set explicitly

Interview note: Trap: passing user.hi as a callback — to setTimeout or an event listener — loses this. Fix it with user.hi.bind(user) or an arrow wrapper () => user.hi().

Q3. What is the difference between call(), apply() and bind()?

All three set this explicitly. call invokes immediately with arguments listed one by one, apply invokes immediately with arguments as an array, and bind returns a new function with this fixed for later.

function greet(greeting, punctuation) {
  return `${greeting}, ${this.name}${punctuation}`;
}
const user = { name: "Siva" };

greet.call(user, "Hello", "!");   // "Hello, Siva!"
greet.apply(user, ["Hi", "?"]);   // "Hi, Siva?"
const hi = greet.bind(user, "Hey");
hi(".");                           // "Hey, Siva."

Q4. What is the difference between a function declaration and a function expression?

A function declaration is fully hoisted and can be called anywhere in its scope. A function expression is assigned to a variable, so only the variable is hoisted — calling it early fails.

greet(); // ✓ works
function greet() { return "Hello"; }

say(); // ✗ TypeError: say is not a function
var say = function () { return "Hi"; };

Q5. What is an IIFE (Immediately Invoked Function Expression)?

An IIFE is a function that is defined and called immediately. It creates a private scope so its variables do not leak into the global scope.

(function () {
  const secret = "hidden";
  console.log("runs immediately");
})();

// Arrow function IIFE
(() => {
  const x = 10;
})();

console.log(typeof secret); // "undefined" — not in global scope

Before ES modules and block-scoped let/const, IIFEs were the standard way to isolate code.

Q6. What is currying in JavaScript?

Currying turns a function that takes several arguments into a chain of functions that each take one argument.

// Without currying
const add = (a, b) => a + b;
add(2, 3); // 5

// Curried
const curriedAdd = (a) => (b) => a + b;
const add5 = curriedAdd(5);
add5(3);  // 8
add5(10); // 15

It is useful for partial application and for building small reusable helpers.

Q7. What are higher-order functions and pure functions?

A higher-order function takes a function as an argument or returns one — map, filter, reduce and setTimeout are everyday examples. A pure function always returns the same output for the same input and changes nothing outside itself.

const nums = [1, 2, 3, 4];
const doubled = nums.map(n => n * 2);          // [2, 4, 6, 8]
const evens = nums.filter(n => n % 2 === 0);   // [2, 4]
const total = nums.reduce((a, n) => a + n, 0); // 10

// Pure: depends only on its input
const add = (a, b) => a + b;

// Impure: changes something outside itself
let count = 0;
const increment = () => count++;

Pure functions are easy to test, cache and reason about — which is why React and Redux push you towards them.

Interview note: Follow-up: "does map change the original array?" No — map, filter and reduce return new values, while sort, reverse and splice change the array in place.

Q8. What is memoization?

Memoization caches a function's result for given inputs, so a repeated call with the same arguments returns the cached value instead of recomputing it.

function memoize(fn) {
  const cache = new Map();
  return function (...args) {
    const key = JSON.stringify(args);
    if (cache.has(key)) return cache.get(key);
    const result = fn(...args);
    cache.set(key, result);
    return result;
  };
}
const fib = memoize((n) => (n <= 1 ? n : fib(n - 1) + fib(n - 2)));
fib(40); // fast

Interview note: Follow-up: "when is memoization a bad idea?" When inputs rarely repeat or the function is cheap — the cache costs memory and the key-building costs time. It also only works for pure functions.

How to prepare

Write these by hand until they are automatic: a counter with a private variable, a once() function, a curried add, and a memoize helper. Then explain, without running it, what each prints.

Next, move to promises and async/await, where closures and this meet asynchronous code, and to OOP and prototypes.

Frequently Asked Questions

What is the simplest way to explain a closure?
A closure is a function that remembers the variables from the place it was created. Show a counter() function that returns an inner function: every call to the inner function still sees and updates the same count variable, even though counter has already finished.
Where are closures used in real projects?
Everywhere: event handlers that remember data, callbacks passed to setTimeout or fetch, module patterns that keep variables private, function factories, memoization caches, and React hooks, which rely on closures over state and props from each render.
What is the difference between call, apply and bind?
All three set this. call and apply invoke the function immediately — call takes arguments one by one, apply takes them as an array. bind does not invoke; it returns a new function with this (and optionally some arguments) fixed for later.
Can closures cause memory leaks?
They can keep large objects alive for as long as the closure itself is reachable, for example a listener or interval that is never removed. The closure is not the bug; forgetting to remove the listener or timer is. Clean them up and the memory is freed.
Why does an arrow function not have its own this?
Arrow functions were designed for short callbacks that should behave like the surrounding code. They take this, arguments and super from the enclosing scope, which removes the need for tricks such as const self = this inside methods.
Is currying asked in interviews?
Often as a follow-up or coding task: write a curried sum, or a generic curry() helper that works for any function. Explain that currying enables partial application — fixing some arguments now and supplying the rest later.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — Check the Java Full Stack training details

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 28 November 2026 LinkedIn
Chat with us