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))prints3 3 3, because every callback closes over the samevar i. Usingletcreates a new binding per iteration and prints0 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.hias a callback — tosetTimeoutor an event listener — losesthis. Fix it withuser.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
mapchange the original array?" No —map,filterandreducereturn new values, whilesort,reverseandsplicechange 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?
Where are closures used in real projects?
What is the difference between call, apply and bind?
Can closures cause memory leaks?
Why does an arrow function not have its own this?
Is currying asked in interviews?
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

