Functions are one of the most asked topics in JavaScript interviews. Most developers know how to create functions, but interview questions usually focus on hoisting, execution behavior, return values, function expressions, arrow functions, and tricky syntax differences.
Question 1
sayHello();
function sayHello() {
console.log("Hello");
}
Function declarations are fully hoisted.
Question 2
sayHello();
var sayHello = function() {
console.log("Hello");
};
The variable is hoisted as undefined. Calling undefined as a function causes a TypeError.
Question 3
const greet = () => {
return "Hi";
};
console.log(greet());
The arrow function returns the string.
Question 4
const add = (a, b) => a + b;
console.log(add(2, 3));
A single expression is returned automatically.
Question 5
const add = (a, b) => {
a + b;
};
console.log(add(2, 3));
When curly braces are used, return must be written explicitly.
Question 6
function test() {
return;
}
console.log(test());
A function without a return value returns undefined.
Question 7
function test() {
return 10;
return 20;
}
console.log(test());
Code after return is never executed.
Question 8
const test = function() {
return 100;
};
console.log(test());
The function expression returns 100.
Question 9
console.log(typeof sayHello);
function sayHello() {}
Function declarations are hoisted with their implementation.
Question 10
console.log(typeof greet);
var greet = function(){};
Only the variable declaration is hoisted.
Question 11
function test(a, b) {
return a + b;
}
console.log(test(5));
b becomes undefined. 5 + undefined is NaN.
Question 12
function test(a = 10) {
return a;
}
console.log(test());
The default parameter is used.
Question 13
const square = n => n * n;
console.log(square(4));
Single parameter arrow functions can omit parentheses.
Question 14
const fn = () => ({name: "John"});
console.log(fn().name);
Parentheses allow implicit object return.
Question 15
const fn = () => { name: "John" };
console.log(fn());
JavaScript treats the braces as a block, not an object.
Question 16
function test() {
console.log("A");
}
console.log(typeof test);
Functions are objects, but typeof returns "function".
Question 17
const test = function abc() {
return "Hello";
};
console.log(test());
Named function expressions work like normal functions.
Question 18
const test = function abc() {};
console.log(typeof abc);
The name is only available inside the function itself.
Question 19
function test() {
return {
value: 10
};
}
console.log(test().value);
The object is returned correctly.
Question 20
function test() {
return
{
value: 10
};
}
console.log(test());
Automatic semicolon insertion places a semicolon after return.
Question 21
function test() {
console.log(arguments.length);
}
test(1, 2, 3);
arguments contains all values passed to the function.
Question 22
const test = () => {
console.log(arguments);
};
test(1, 2);
Arrow functions do not have their own arguments object.
Question 23
function test(a, b, c) {}
console.log(test.length);
length returns the number of declared parameters.
Question 24
const add = (...nums) => nums.length; console.log(add(1,2,3,4));
Rest parameters collect all remaining arguments.
Question 25
function test() {
return typeof arguments;
}
console.log(test());
arguments is an object-like structure.
Question 26
const obj = {
value: 10,
getValue: () => {
return this.value;
}
};
console.log(obj.getValue());
Arrow functions do not get their own this.
Question 27
const obj = {
value: 10,
getValue() {
return this.value;
}
};
console.log(obj.getValue());
Regular methods get this from the caller object.
Question 28
const User = (name) => {
this.name = name;
};
new User("John");
Arrow functions cannot be used as constructors.
Question 29
function User(name) {
this.name = name;
}
const user = new User("John");
console.log(user.name);
Regular functions can act as constructors.
Question 30
(function() {
console.log("Hello");
})();
This is an Immediately Invoked Function Expression.
Question 31
const fn = function() {
return function() {
return "Hello";
};
};
console.log(fn()());
The first call returns a function. The second executes it.
Question 32
function outer() {
return () => "Hi";
}
console.log(outer()());
A function can return another function.
Question 33
const test = x => y => x + y; console.log(test(5)(3));
This is a simple currying example.
Question 34
function factorial(n) {
if(n === 1) return 1;
return n * factorial(n - 1);
}
console.log(factorial(4));
The function calls itself recursively.
Question 35
function test(callback) {
callback();
}
test(() => console.log("Done"));
Functions can be passed as arguments.
Question 36
const numbers = [1,2,3];
const result=numbers.map(function(n) {
return n * 2;
});
console.log(result)
map creates a new transformed array.
Question 37
const numbers = [1,2,3]; const result = numbers.map(n => n * 2); console.log(result[1]);
The second element becomes 4.
Question 38
function test() {
return function() {
return function() {
return "Nested";
};
};
}
console.log(test()()());
Each function returns another function until the final value.
Question 39
const sum = (...nums) =>
nums.reduce((a, b) => a + b, 0);
console.log(sum(1,2,3));
The rest parameter collects values and reduce adds them.
Question 40
console.log(
(() => typeof (() => {}))()
);
Arrow functions still have type "function".