Why does second have access to variable a?
The answer is B because JavaScript uses lexical scope.
When a function is created inside another function, the inner function can access variables from the outer function where it was defined.
For example:
function first() {
let a = 10;
function second() {
console.log(a);
}
second();
}
Here, a belongs to the outer function first(). The inner function second() can access it because JavaScript looks for variables through the scope chain.
The lookup works like this:
1. Look inside second()
2. If not found, look inside first()
3. If not found, look in the global scope
Since a is found in the outer function's scope, second() can use it.
This is also the basis of a closure. A closure allows an inner function to remember and access variables from its outer lexical environment.
Why the other options are incorrect:
A is not the best answer because a is not declared inside second() itself.
C is incorrect because the prototype chain is used for object property lookup, not local variable scope lookup.
D is incorrect because hoisting explains how declarations are moved during compilation, but it does not explain why an inner function can access an outer function's variable.
Therefore, the verified answer is B.
Refer to the code below:
01
02
| Click me! |
07
08 function printMessage(event) {
09 console.log('Row log');
10 event.stopPropagation();
11 }
12
13 let elem = document.getElementById('row1');
14 elem.addEventListener('click', printMessage, false);
15
16
Which code change should be done for the console to log the following when "Click me!" is clicked?
Row log
Table log
Current behavior:
Clicking <td> triggers the click event on row1, then bubbles up to <table>.
printMessage runs, logs 'Row log', then event.stopPropagation() stops the event from bubbling to the table.
So 'Table log' never appears.
To allow the table's inline onclick to run after the row handler:
Remove the propagation stop:
function printMessage(event) {
console.log('Row log');
// event.stopPropagation(); // remove this
}
Now the event bubbles:
printMessage logs 'Row log'.
The table's onclick runs, logging 'Table log'.
Option A only changes capture/bubble phase but still stops propagation. C does nothing meaningful (stopPropagation takes no arguments). B removes the row handler entirely.
Refer to the code:
01 function execute() {
02 return new Promise((resolve, reject) => reject());
03 }
04 let promise = execute();
05
06 promise
07 .then(() => console.log('Resolved1'))
08 .then(() => console.log('Resolved2'))
09 .then(() => console.log('Resolved3'))
10 .catch(() => console.log('Rejected'))
11 .then(() => console.log('Resolved4'));
What is the result when the Promise in the execute function is rejected?
execute() returns a Promise that immediately calls reject().
So promise starts in a rejected state.
When a Promise is rejected and you chain .then() calls without rejection handlers, all those .then() callbacks are skipped until a .catch() is encountered:
promise
.then(...) // skipped
.then(...) // skipped
.then(...) // skipped
.catch(...) // executed
.then(...); // executed after catch
Execution:
.then(() => console.log('Resolved1')) is skipped.
.then(() => console.log('Resolved2')) is skipped.
.then(() => console.log('Resolved3')) is skipped.
.catch(() => console.log('Rejected')) runs and logs Rejected.
The .catch() returns a resolved Promise (no explicit return, so undefined), so the next .then() runs:
.then(() => console.log('Resolved4')) logs Resolved4.
Final output:
Rejected
Resolved4
This matches option D.
Refer to the following object:
const dog = {
firstName: 'Beau',
lastName: 'Boo',
get fullName() {
return this.firstName + ' ' + this.lastName;
}
};
How can a developer access the fullName property for dog?
The correct answer is A.
This object uses a getter:
get fullName() {
return this.firstName + ' ' + this.lastName;
}
A getter looks like a method when it is defined, but it is accessed like a normal property.
So the correct access syntax is:
dog.fullName
That returns:
'Beau Boo'
The important distinction is:
dog.fullName
not:
dog.fullName()
Because fullName is a getter property, not a regular function property.
Option B is incorrect because calling dog.fullName() tries to call the returned string as a function. Since 'Beau Boo' is not a function, that would cause a TypeError.
Option C is incorrect because there is no get object inside dog.
Option D is incorrect because there is no function object inside dog, and getters are not accessed that way.
Therefore, the verified answer is A.
Refer to the code below:
01 const myFunction = arr => {
02 return arr.reduce((result, current) => {
03 return result + current;
04 }, 10);
05 }
What is the output of this function when called with an empty array?
We call:
myFunction([]);
Inside:
arr.reduce((result, current) => {
return result + current;
}, 10);
Key points about Array.prototype.reduce:
Signature: array.reduce(callback, initialValue)
If initialValue is provided and the array is empty, reduce:
Does not call the callback at all.
Simply returns initialValue.
Here:
arr is [] (empty).
initialValue is 10.
So:
No iterations of the callback happen (no elements to process).
The return value is the initial value: 10.
So the actual output is:
myFunction([]) === 10;
Among the options:
A: 0 -- incorrect, because the initial value is 10, not 0.
B: Throws an error -- reduce throws only if the array is empty and there is no initialValue. Here we have an initial value, so no error.
C: NaN -- there is no arithmetic with undefined or invalid values; we just return 10.
D: Returns 5 -- the numeric value given is wrong; the correct value is 10.
Given the logic, the correct conceptual result is 10. The option text ''Returns 5'' is almost certainly a typo for ''Returns 10''. Since the letter that is intended to represent the correct behavior is D, we keep:
Answe r: D
Study Guide / Concept Reference (no links):
Array.prototype.reduce behavior with initialValue
Behavior of reduce on an empty array with and without initialValue
Return value when no iterations run
Heather Moore
26 days agoAdam Flores
28 days agoAmanda Cooper
2 months agoAmy Thomas
2 months agoDavid Jackson
3 months agoKaren Jones
3 months agoCharles Carter
4 months agoHeather Bell
4 months agoBrenda Clark
4 months agoDeborah Mitchell
4 months agoJeffrey Bailey
4 months agoRobert Anderson
4 months agoGerald Ramirez
4 months agoMalcom
5 months agoZoila
5 months agoMicaela
5 months agoKendra
6 months agoPhyliss
6 months agoLeandro
6 months agoHelga
6 months agoBrent
7 months ago