Functions Questions with Answers

7 practice questions on Functions (JavaScript) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

function add(a, b) { return a + b; }
console.log(add(1), add(1, 2, 3));
  1. NaN, 3
  2. 1, 3
  3. NaN, 6
  4. undefined, 3

Answer: A. NaN, 3

Explanation

A missing b is undefined, so 1 + undefined = NaN; extra arguments are ignored.

Step-by-step solution

  1. add(1): b is undefined, 1 + undefined = NaN.
  2. add(1, 2, 3): the extra 3 is ignored.

Why the other options are wrong

  • B. Missing parameters are undefined.
  • C. Extra arguments are ignored by the named parameters.
  • D. 1 + undefined is NaN.

Question 2Easy

What is the output?

function f() { return arguments.length; }
console.log(f(), f(1, 2, 3), f(undefined));
  1. 0, 3, 1
  2. 0, 3, 0
  3. 0, 3, 2
  4. 1, 3, 1

Answer: A. 0, 3, 1

Explanation

arguments.length counts passed arguments, even undefined.

Step-by-step solution

  1. No arguments: 0.
  2. Three arguments: 3.
  3. undefined is still passed: 1.

Why the other options are wrong

  • B. Passing undefined still counts as an argument.
  • C. Only one argument is passed.
  • D. Zero arguments gives length 0.

Question 3Medium

What is the output?

function make() {
  let n = 0;
  return () => ++n;
}
const a = make(), b = make();
a(); a();
console.log(a(), b());
  1. 3 3
  2. 2 1
  3. 3 1
  4. 1 1

Answer: C. 3 1

Explanation

Each call to make() creates a new closure with its own n. a is called three times in total, so its n reaches 3. b is called once, so its n reaches 1.

Step-by-step solution

  1. make() runs twice, giving a and b separate variables n.
  2. a() is called three times in total, so its n goes 1, 2, 3.
  3. b() is called once, so its n is 1; the log prints 3 1.

Why the other options are wrong

  • A. a and b keep separate counters, so b is not 3.
  • B. a has been called three times by the time of the log.
  • D. a's counter has reached 3, not 1.

Tip: Each call to a factory function gives the returned closure its own copy of the local variables.

Question 4Medium

What is the output?

function f(x, y = x * 2) {
  x = 10;
  return y;
}
console.log(f(3));
  1. 10
  2. 3
  3. 6
  4. 20

Answer: C. 6

Explanation

A default value is evaluated when the call happens, using the arguments so far. y defaults to 3 * 2 = 6. Changing x later in the body does not change y.

Step-by-step solution

  1. f(3) sets x = 3, and y is not passed, so its default is used.
  2. The default x * 2 is evaluated at call time with x = 3, so y = 6.
  3. x = 10 runs afterwards, but y has already been set to 6.

Why the other options are wrong

  • A. x is 10 by then, but y was already set from the old x.
  • B. y is the default, which doubles x; it is not x.
  • D. Using 10 * 2 assumes the default is evaluated after x changes.

Tip: Default parameters are evaluated left to right when the function is called.

Question 5Medium

What is the output?

console.log(sayHi());
function sayHi() { return 'hi'; }
  1. undefined
  2. sayHi
  3. ReferenceError
  4. hi

Answer: D. hi

Explanation

Function declarations are hoisted: the whole function exists before the first line runs. So sayHi() can be called above its declaration and returns 'hi'.

Step-by-step solution

  1. A function declaration is hoisted to the top of its scope.
  2. sayHi is defined before console.log runs.
  3. Calling it returns the string 'hi'.

Why the other options are wrong

  • A. Only the var declaration would be hoisted as undefined; a function declaration is fully hoisted.
  • B. sayHi() calls the function; the name itself is not printed.
  • C. Hoisting makes the name available, so no ReferenceError is thrown.

Tip: Function declarations hoist with their body; let and const do not.

Question 6Hard

What is the output?

function greet(g, p) {
  return g + ', ' + this.name + p;
}
const o = { name: 'Ana' };
const bound = greet.bind(o, 'Yo');
console.log(bound.call({ name: 'Bo' }, '?'));
  1. Yo, Bo?
  2. Ana, Yo?
  3. undefined, Ana?
  4. Yo, Ana?

Answer: D. Yo, Ana?

Explanation

bind fixes this to o and the first argument to 'Yo'. A bound function ignores a later call or apply that tries to change this. So this.name is 'Ana', the second argument '?' fills p, and the result is 'Yo, Ana?'.

Step-by-step solution

  1. bind(o, 'Yo') fixes this = o and g = 'Yo'.
  2. bound.call({ name: 'Bo' }, '?') tries to set this to Bo, but a bound function keeps its fixed this.
  3. So this.name is 'Ana' and p is '?', giving 'Yo, Ana?'.

Why the other options are wrong

  • A. A bound function keeps the this given to bind, so the call cannot switch it to Bo.
  • B. The first argument is g, so 'Yo' comes first.
  • C. g was fixed by bind to 'Yo', so it is not undefined.

Tip: bind is permanent: once bound, this cannot be changed by call, apply or bind again.

Question 7Hard

What is the output?

function memo(fn) {
  const cache = {};
  return n => (n in cache ? cache[n] : (cache[n] = fn(n)));
}
let calls = 0;
const sq = memo(n => { calls++; return n * n; });
sq(3); sq(3); sq(4); sq(3);
console.log(calls);
  1. 4
  2. 2
  3. 1
  4. 3

Answer: B. 2

Explanation

Only the distinct arguments 3 and 4 call the function; repeated calls hit the cache.

Step-by-step solution

  1. sq(3) computes and caches.
  2. Second sq(3) is cached.
  3. sq(4) computes.
  4. Last sq(3) is cached. calls = 2.

Why the other options are wrong

  • A. Repeated arguments are served from the cache.
  • C. A different argument (4) is computed.
  • D. Only distinct arguments call the function.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all JavaScript topics · All topics