Given a value, which two options can a developer use to detect if the value is NaN?
Answer : C, D
Comprehensive and Detailed Explanation From Exact Extract JavaScript knowledge:
We already know NaN is special: it is not equal to itself.
Check each:
A . value === Number.NaN
Number.NaN is NaN.
NaN === NaN is always false.
This will never be true; cannot reliably detect NaN.
B . value == NaN
NaN == NaN is also always false.
Again, this never detects NaN.
C . isNaN(value)
Global isNaN converts its argument to a number and then checks if the result is NaN.
This can detect NaN, but it may also return true for non-number values that coerce to NaN, such as isNaN('foo').
Regardless, it is a standard way to detect if a value is ''NaN-like'' in JavaScript.
D . Object.is(value, NaN)
Object.is(NaN, NaN) returns true.
This is a strict way to detect a value that is exactly NaN (no coercion).
Therefore, among the given choices, the two viable ways to detect NaN are:
isNaN(value)
Object.is(value, NaN)
________________________________________
Refer to the code below:
01 function myFunction(reassign) {
02 let x = 1;
03 var y = 1;
04
05 if (reassign) {
06 let x = 2;
07 var y = 2;
08 console.log(x);
09 console.log(y);
10 }
11
12 console.log(x);
13 console.log(y);
14 }
What is displayed when myFunction(true) is called?
Answer : B
Comprehensive and Detailed Explanation From Exact Extract JavaScript knowledge:
This question tests understanding of let (block scope) and var (function scope) in JavaScript.
Initial declarations in the function:
let x = 1; // line 2
var y = 1; // line 3
Here:
x is declared with let, so it is block-scoped to the function body.
y is declared with var, so it is function-scoped to the entire function.
Inside the if (reassign) block (and since reassign is true, we enter it):
if (reassign) {
let x = 2; // line 6
var y = 2; // line 7
console.log(x); // line 8
console.log(y); // line 9
}
Detailed behavior:
let x = 2; on line 6 creates a new block-scoped variable x that exists only inside the if block. It does not change the outer x declared on line 2.
var y = 2; on line 7 declares y with var again, but var is function-scoped. This effectively reassigns the same y defined on line 3 for the entire function. After this line, y is 2 everywhere in the function.
Now, inside the if block:
console.log(x); (line 8) logs the inner block-scoped x, which is 2.
console.log(y); (line 9) logs y, which is the function-scoped y that was set to 2.
So the first two outputs are:
2
2
After the if block, execution continues:
console.log(x); // line 12
console.log(y); // line 13
Outside the if block:
The block-scoped let x = 2; no longer exists; it was only visible inside the if block.
The outer let x = 1; (line 2) is still in scope and has not been changed.
Thus:
console.log(x); (line 12) logs the outer x, which is still 1.
console.log(y); (line 13) logs y which, due to var y = 2; inside the if, is now 2 for the whole function.
Therefore, when myFunction(true) is called, the output in order is:
2 (inner x in if)
2 (function-scoped y after reassignment)
1 (outer x after if)
2 (function-scoped y remains 2)
This corresponds to:
JavaScript knowledge / study guide reference concepts:
let declarations and block scope
var declarations and function scope
Shadowing of variables with let inside a block
Re-declaration and reassignment of var within a function
Execution order of statements and console output
________________________________________
Refer to the code below:
```html
function printMessage() {
console.log('This is a test message');
}
let el = document.getElementById('test');
el.addEventListener("click", printMessage, false);
Answer : C
Refer to the code below:
01 const objBook = {
02 title: 'JavaScript',
03 };
04 Object.preventExtensions(objBook);
05 const newObjBook = objBook;
06 newObjBook.author = 'Robert';
What are the values of objBook and newObjBook respectively?
Answer : A
________________________________________
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge
Object.preventExtensions(obj)
This built-in JavaScript method marks an object so that no new properties can be added to it.
Existing properties can still be read and updated, but adding new ones is disallowed.
const newObjBook = objBook;
Both variables reference the same object in memory. JavaScript objects are assigned by reference, not copied.
newObjBook.author = 'Robert';
Because the object has been marked as non-extensible, JavaScript will not allow new properties to be added.
The behavior depends on mode:
In non-strict mode: the assignment silently fails and does nothing.
In strict mode: this would throw a TypeError.
Since nothing indicates strict mode, this is non-strict behavior, making the assignment fail silently.
Therefore, the object remains:
{ title: 'JavaScript' }
Both objBook and newObjBook point to the same unchanged object.
This matches option A.
________________________________________
JavaScript knowledge references (text-only)
Object.preventExtensions() prevents adding new properties.
Assigning an object to another variable copies the reference, not the object.
Adding a property to a non-extensible object silently fails in non-strict mode.
==================================================
Refer to the code below:
01 const addBy = ?
02 const addByEight = addBy(8);
03 const sum = addByEight(50);
Which two functions can replace line 01 and return 58 to sum?
Answer : A, D
Comprehensive and Detailed Explanation From Exact Extract JavaScript knowledge:
We want:
const addByEight = addBy(8);
const sum = addByEight(50);
And we need sum to be 58.
That means:
addBy(8) must return a function.
That returned function, when called with 50, must compute 8 + 50.
So addBy must be a higher-order function that returns another function capturing num1 and later using it with num2 (a closure).
Option A
const addBy = function(num1) {
return function(num2) {
return num1 + num2;
}
}
Step-by-step:
Call addBy(8):
num1 is 8.
The function returns an inner function: function(num2) { return num1 + num2; }.
So addByEight becomes this inner function, with num1 closed over as 8.
Then call addByEight(50):
num2 is 50.
The body computes num1 + num2, i.e., 8 + 50 = 58.
Therefore, with Option A in place:
const addByEight = addBy(8); // returns inner function
const sum = addByEight(50); // 58
So sum is 58. Option A is correct.
Option D (corrected)
const addBy = (num1) => {
return function(num2) {
return num1 + num2;
}
}
This is essentially the same logic expressed with an arrow function for the outer function:
Call addBy(8):
num1 is 8.
Returns the inner function function(num2) { return num1 + num2; }.
Call addByEight(50):
num2 is 50.
Computes num1 + num2 8 + 50 = 58.
So again:
const addByEight = addBy(8); // inner function with num1 = 8
const sum = addByEight(50); // 58
Option D is also correct.
Thus, A and D are the two functions that satisfy the requirement.
Why B and C are incorrect
Option B:
const addBy = function(num1) {
return num1 * num2;
}
This does not return a function; it returns a value.
addBy(8) returns 8 * num2, but num2 is not defined in this scope, which would cause a ReferenceError.
Also, addByEight would be a number (if num2 existed), not a function, so addByEight(50) would fail.
Option C:
const addBy = (num1) => num1 + num2;
Again, addBy returns a value, not a function.
addBy(8) returns 8 + num2, but num2 is not defined, so this is also invalid due to ReferenceError.
addByEight would be a number (or error), not a function.
Neither B nor C creates the required closure nor returns a function to be called later.
Reference / Study Guide concepts (no links):
Higher-order functions in JavaScript
Closures: inner functions capturing outer variables (num1)
Arrow functions vs function expressions
Returning functions from functions (function factories / currying)
Scope and ReferenceError when a variable is not defined
________________________________________
A developer implements a function that adds a few values.
```javascript
function sum(num1, num2, num3) {
if (num3 === undefined) {
num3 = 0;
}
return num1 + num2 + num3;
}
```
Which three options can the developer invoke for this function to get a return value of 10?
Choose 3 answers
Answer : A, B
Refer to the code below:
01 new Promise((resolve, reject) => {
02 const fraction = Math.random();
03 if (fraction > 0.5) reject('fraction > 0.5, ' + fraction);
04 resolve(fraction);
05 })
06 .then(() => console.log('resolved'))
07 .catch((error) => console.error(error))
08 .finally(() => console.log('when am I called?'));
When does Promise.finally on line 08 get called?
Answer : D
Comprehensive and Detailed Explanation From Exact Extract JavaScript knowledge:
Behavior of Promise.prototype.finally:
.finally(handler) registers a callback that runs when the promise is settled, meaning:
Either fulfilled (resolved), or
rejected.
Important points:
The finally callback does not receive the promise's value or error (unlike then and catch).
It is executed after the promise is settled, but before the resolution value or rejection reason is passed further down the chain.
It runs in both success and failure paths.
In the given code:
The promise may either:
Call reject('fraction > 0.5, ' + fraction) if fraction > 0.5, or
Call resolve(fraction) otherwise.
In both cases:
If it resolves, .then(() => console.log('resolved')) runs, and then .finally(...) is executed.
If it rejects, .catch((error) => console.error(error)) runs, and then .finally(...) is executed.
So .finally runs:
Not just ''when rejected''.
Not just ''when resolved''.
But whenever the promise is resolved or rejected.
Therefore, the correct choice is:
D . When resolved or rejected.