Functions in JavaScript — Interview Questions & Answers
Ques: What is a Function in JavaScript?
Ans: A function is a reusable block of code designed to perform a specific task. It executes when called or invoked.
function greet() {
console.log("Hello!");
}
greet(); // Hello!
Ques: What are the different ways to define a function in JavaScript?
- Function Declaration
- Function Expression
- Arrow Function
- Constructor Function
- IIFE (Immediately Invoked Function Expression)
Ques: What is a Function Declaration?
Ans: Defined using the function keyword, it can be hoisted.
function add(a, b) {
return a + b;
}
Ques: What is a Function Expression?
Ans: A function assigned to a variable — not hoisted.
const multiply = function(a, b) {
return a * b;
};
Ques: What is the difference between Function Declaration and Expression?
| Feature | Declaration | Expression |
|---|---|---|
| Hoisting | Yes | No |
| Syntax | Named | Can be anonymous |
| Use case | Define functions early | Dynamic assignment |
Ques: What is an Anonymous Function?
Ans: A function without a name, often used in callbacks or IIFEs.
setTimeout(function() {
console.log("Anonymous!");
}, 1000);
Ques: What is an IIFE (Immediately Invoked Function Expression)?
Ans: A function that executes immediately after creation.
(function() {
console.log("IIFE runs instantly!");
})();
Ques: What are Parameters and Arguments?
- Parameters → variables listed in function definition.
- Arguments → actual values passed when calling the function.
function greet(name) { // parameter
console.log("Hi, " + name);
}
greet("Riya"); // argument
Ques: What are Default Parameters?
Ans: Used to assign a default value when no argument is provided.
function greet(name = "Guest") {
console.log("Hi, " + name);
}
greet(); // Hi, Guest
Ques: What is the Rest Parameter (...)?
Ans: Collects multiple arguments into an array.
function sum(...nums) {
return nums.reduce((a, b) => a + b);
}
sum(1, 2, 3); // 6
Ques: What is the Spread Operator (...) in functions?
Ans: Used to spread array elements as separate arguments.
const nums = [1, 2, 3];
console.log(Math.max(...nums)); // 3
Que: What is Function Invocation?
Ans: Executing a function using ().
function sayHi() { console.log("Hi!"); }
sayHi(); // invoked
Ques: What are the different ways to invoke a function?
- Direct invocation
- Method invocation
- Constructor invocation (
new) - Indirect invocation (
call,apply,bind)
Ques: What is Direct Invocation?
Ans: Calling a function directly by name.
function test() { console.log(this); }
test(); // this → window / undefined (strict)
Ques: What is Method Invocation?
Ans: When a function is called as a property of an object.
const obj = {
name: "Dev",
greet() { console.log("Hi " + this.name); }
};
obj.greet(); // Hi Dev
Ques: What are Arrow Functions?
Ans: Introduced in ES6 — shorter syntax, no own this, arguments, or super.
const add = (a, b) => a + b;
Ques: What is Lexical this in Arrow Functions?
Ans: Arrow functions don’t bind this — they inherit from their parent scope.
function Counter() {
this.count = 0;
setInterval(() => console.log(++this.count), 1000);
}
new Counter(); // works correctly
Ques: What are Higher-Order Functions?
Ans: Functions that take other functions as arguments or return them.
function calculate(fn, x, y) {
return fn(x, y);
}
calculate((a,b)=>a+b, 2, 3); // 5
Ques: What are Callback Functions?
Ans: Functions passed as arguments to other functions.
function greet(callback) {
console.log("Hello!");
callback();
}
greet(() => console.log("Callback executed"));
Ques: What are Async Functions?
Ans: Functions that use async/await to handle asynchronous code.
async function fetchData() {
const res = await fetch("<https://api.example.com>");
return res.json();
}
Ques: What is a Generator Function?
Ans: A function that can pause execution using yield.
function* gen() {
yield 1;
yield 2;
}
const g = gen();
console.log(g.next().value); // 1
Ques: What is Function Scope?
Ans: Variables declared inside a function are local to that function.
function demo() {
let x = 10;
console.log(x);
}
console.log(x); // ReferenceError
Ques: What is Function Hoisting?
Ans: Function declarations are hoisted — you can call them before defining.
greet();
function greet() {
console.log("Hoisted!");
}
Ques: What are Closures?
Ans: A closure is when an inner function remembers variables from its outer function even after it has returned.
function outer() {
let count = 0;
return function inner() {
count++;
console.log(count);
};
}
const counter = outer();
counter(); // 1
counter(); // 2
Ques: Why are Closures useful?
- Data privacy
- State management
- Function factories
Ques: What is the this keyword?
Ans: Refers to the object that owns the function being executed.
Ques: What is this in regular functions vs arrow functions?
| Function Type | this Value |
|---|---|
| Regular | Dynamic (depends on call site) |
| Arrow | Lexical (from outer scope) |
Ques: Explain Call, Apply, and Bind.
call()→ invoke immediately, pass args individuallyapply()→ invoke immediately, pass args as arraybind()→ returns a new bound function
function show(city) {
console.log(this.name, city);
}
const obj = { name: "Alex" };
show.call(obj, "Delhi");
show.apply(obj, ["Goa"]);
const bound = show.bind(obj, "Mumbai");
bound();
Ques: What is Recursion?
Ans: When a function calls itself until a condition is met.
function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
Ques: What is Tail Recursion?
Ans: A recursion where the recursive call is the last statement. Optimizes stack usage.
Ques: What are Pure Functions?
Ans: Functions that always produce the same output for the same input and have no side effects.
function add(a, b) {
return a + b;
}
Ques: What are Side Effects in functions?
When a function modifies something outside its scope (e.g., global variables, DOM).
let count = 0;
function increment() {
count++; // side effect
}
Ques: How can you avoid side effects?
- Use pure functions
- Avoid mutating global data
- Use immutable data patterns
Ques: What is Function Composition?
Ans: Combining multiple functions to produce a new one.
const compose = (f, g) => x => f(g(x));
const add = x => x + 2;
const double = x => x * 2;
console.log(compose(add, double)(3)); // 8
Ques: What are Function Constructors?
Ans: Using new Function() to create functions dynamically (not recommended).
const add = new Function('a', 'b', 'return a + b');
Ques: What is Currying in JavaScript?
Ans: Breaking down a function with multiple arguments into a sequence of single-argument functions.
function curry(a) {
return b => a + b;
}
curry(5)(3); // 8
Ques: What are First-Class Functions?
Ans: Functions treated like variables — can be passed, returned, or stored.
Ques: Difference between Named and Anonymous Function Expressions?
- Named: helps in debugging.
- Anonymous: used for inline callbacks.
const named = function test() {};
const anon = function() {};
Ques: Why are Functions called Objects in JavaScript?
Ans: Because they can have properties and methods (like .name, .length, .call()).
function f(a,b){}
console.log(f.length); // 2
