Question 1Easy
What is the output?
function add(a, b) { return a + b; }
console.log(add(1), add(1, 2, 3));
- NaN, 3
- 1, 3
- NaN, 6
- undefined, 3
Answer: A. NaN, 3
Explanation
A missing b is undefined, so 1 + undefined = NaN; extra arguments are ignored.
Step-by-step solution
- add(1): b is undefined, 1 + undefined = NaN.
- 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));
- 0, 3, 1
- 0, 3, 0
- 0, 3, 2
- 1, 3, 1
Answer: A. 0, 3, 1
Explanation
arguments.length counts passed arguments, even undefined.
Step-by-step solution
- No arguments: 0.
- Three arguments: 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());
- 3 3
- 2 1
- 3 1
- 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
- make() runs twice, giving a and b separate variables n.
- a() is called three times in total, so its n goes 1, 2, 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));
- 10
- 3
- 6
- 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
- f(3) sets x = 3, and y is not passed, so its default is used.
- The default x * 2 is evaluated at call time with x = 3, so y = 6.
- 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'; }
- undefined
- sayHi
- ReferenceError
- 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
- A function declaration is hoisted to the top of its scope.
- sayHi is defined before console.log runs.
- 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' }, '?'));
- Yo, Bo?
- Ana, Yo?
- undefined, Ana?
- 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
- bind(o, 'Yo') fixes this = o and g = 'Yo'.
- bound.call({ name: 'Bo' }, '?') tries to set this to Bo, but a bound function keeps its fixed this.
- 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.