JS-Dev-101 Exam Questions & Answers
Salesforce Certified JavaScript Developer • Salesforce
100% money-back guarantee
Sample JS-Dev-101 Questions
Practice with real exam-style questions, each with the verified correct answer and explanation.
A developer implements a function that adds a few values.
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?
The verified corrected answers are C, D, and E.
This function is a normal function, not a curried function:
function sum(num1, num2, num3) {
if (num3 === undefined) {
num3 = 0;
}
return num1 + num2 + num3;
}
It expects values to be passed in the same function call:
sum(num1, num2, num3);
Now check the valid corrected options.
Option C:
sum(5, 5, 0);
Calculation:
5 + 5 + 0
Result:
10
So C is correct.
Option D:
sum(10, 0);
Here, num3 is not provided, so it is undefined.
The function checks:
if (num3 === undefined) {
num3 = 0;
}
So the calculation becomes:
10 + 0 + 0
Result:
10
So D is correct.
Option E:
sum(5, 2, 3);
Calculation:
5 + 2 + 3
Result:
10
So E is correct.
Why A and B are incorrect as originally written:
sum(5)(5);
This calls sum(5) first. Since num2 is missing, the result becomes NaN. Then JavaScript tries to call that returned value as a function, which causes a TypeError.
sum()(10);
This also calls sum() first, producing NaN, and then attempts to call NaN as a function.
Those styles would only work if sum were written as a curried function, but the given implementation is not curried.
Therefore, the verified corrected answers are C, D, and E.
static delay = async delay => {
return new Promise(resolve => {
setTimeout(resolve, delay);
});
};
static asyncCall = async () => {
await delay(1000);
console.log(1);
};
console.log(2);
asyncCall();
console.log(3);
Assume delay and asyncCall are in scope as functions.
What is logged to the console?
The correct answer is D, because JavaScript executes synchronous code first, while the code after await runs later after the Promise resolves.
The execution order is:
console.log(2);
This runs first, so JavaScript logs:
2
Then this line runs:
asyncCall();
The asyncCall() function starts executing. Inside it, JavaScript reaches:
await delay(1000);
The delay(1000) function returns a Promise that resolves after 1000 milliseconds:
return new Promise(resolve => {
setTimeout(resolve, delay);
});
Because await pauses the rest of the asyncCall() function, this line does not run immediately:
console.log(1);
Instead, JavaScript continues executing the remaining synchronous code outside the async function:
console.log(3);
So JavaScript logs:
3
After approximately 1000 milliseconds, the Promise returned by delay(1000) resolves. Then the paused async function continues and runs:
console.log(1);
So JavaScript logs:
1
Final console output:
2
3
1
Important JavaScript concepts involved:
An async function always returns a Promise.
The await keyword pauses execution only inside the async function where it appears.
Code outside the async function continues running normally.
setTimeout() schedules its callback to run later, after the current synchronous code has finished.
Therefore, the verified answer is D. 2 3 1.
Correct implementation of try...catch for countsDeep():
The correct answer is B because countsDeep() is executed inside the callback function passed to setTimeout(), and the try...catch block is also placed inside that same callback.
In JavaScript, setTimeout() schedules a function to run later. The outer code finishes first, and the callback runs asynchronously after the delay. Because of this, a try...catch block placed outside setTimeout() cannot catch errors thrown later inside the callback.
Correct logic:
setTimeout(function() {
try {
countsDeep();
} catch (e) {
handleError(e);
}
}, 1000);
Here, when countsDeep() runs after 1000 milliseconds, any error thrown by countsDeep() happens inside the try block. Therefore, the catch (e) block can catch that error and pass it to handleError(e).
Why the other options are incorrect:
A is incorrect because the syntax is invalid JavaScript. A valid try...catch structure must be:
try {
// code
} catch (e) {
// handle error
}
Option A incorrectly writes:
} handleError (e){
catch(e);
}
That is not valid try...catch syntax.
C is incorrect because the try...catch surrounds only the call to setTimeout(), not the later execution of countsDeep(). If countsDeep() throws an error after the timer expires, the outer catch block will not catch it.
D is incorrect for the same reason as C. In the original question, D also had a typing error: it used countSheep() instead of countsDeep(). Even after correcting that typing error, D is still incorrect because the try...catch is outside the asynchronous callback.
Refer to the following code:
01 let obj = {
02 foo: 1,
03 bar: 2
04 }
05 let output = []
06
07 for (let something of obj) {
08 output.push(something);
09 }
10
11 console.log(output);
What is the value of output on line 11?
The key line is:
for (let something of obj) {
In JavaScript:
for...of is used to iterate over iterable objects, such as:
Arrays
Strings
Maps
Sets
Other objects that implement a [Symbol.iterator] method.
Plain JavaScript objects created with object literal {} are not iterable by default. They do not have [Symbol.iterator], so using for...of directly on them causes a runtime error.
Specifically:
for (let something of obj) { ... }
will throw a TypeError similar to:
obj is not iterable
Therefore, the loop body never executes, and console.log(output); is never reached without an error.
Why other options are incorrect:
B . [1, 2]
To get [1, 2], you could use Object.values(obj) and iterate that array.
But here, for...of obj never yields values because it throws an error.
C . ['foo', 'bar']
To get property names, you could use Object.keys(obj) with for...of.
Again, the code does not do that; it incorrectly tries to iterate the object directly.
D . ['foo:1', 'bar:2']
You would need both keys and values, combining them manually.
The given code does not implement such logic and fails before pushing anything into output.
Hence, the correct answer is:
Answe r: A
Study Guide / Concept Reference (no links):
Difference between for...of and for...in
Iterables in JavaScript and [Symbol.iterator]
Plain objects {} are not iterable by default
Correct patterns to iterate object keys/values (Object.keys, Object.values, Object.entries)
Refer to the code below:
let inArray = [ [1, 2], [3, 4, 5] ];
Which two statements result in the array [1, 2, 3, 4, 5]?
(With corrected typing errors: usArray inArray, .. ....)
We start with the array:
let inArray = [ [1, 2], [3, 4, 5] ];
This is an array of two inner arrays:
First element: [1, 2]
Second element: [3, 4, 5]
The desired result is to transform this into a single, flat array:
[1, 2, 3, 4, 5]
This is accomplished by using Array.prototype.concat to concatenate the inner arrays into one new array, optionally combined with the spread syntax (...) or Function.prototype.apply.
Option A: [].concat(...inArray);
Relevant concepts:
Spread syntax (...inArray) expands an iterable into separate arguments.
Array.prototype.concat joins arrays or values into a new array. When you pass arrays as arguments to concat, it flattens them one level into the result.
Step-by-step behavior:
inArray is [ [1, 2], [3, 4, 5] ].
...inArray expands into [1, 2] and [3, 4, 5] as separate arguments.
So [].concat(...inArray) is equivalent to:
[].concat([1, 2], [3, 4, 5]);
concat processes each argument:
For [1, 2], it adds 1 and 2 into the result array.
For [3, 4, 5], it adds 3, 4, and 5 into the result array.
The final outcome is:
[1, 2, 3, 4, 5]
Therefore, Option A correctly produces the desired array.
Option B: [].concat.apply(inArray, []);
Corrected name from usArray to inArray.
Relevant concepts:
Function.prototype.apply(fnThis, argsArray) invokes a function with a specific this value and a list of arguments passed as an array.
Here:
thisArg is inArray.
argsArray is [] (no actual arguments passed).
So the call:
[].concat.apply(inArray, []);
is equivalent to:
Array.prototype.concat.apply(inArray, []);
// i.e. inArray.concat();
Since there are no extra arguments, inArray.concat() simply returns a shallow copy of inArray:
[ [1, 2], [3, 4, 5] ]
This remains an array of arrays and is not flattened into [1, 2, 3, 4, 5]. Therefore, Option B does not produce the required result.
Option C: [].concat::...inArray();
Corrected name from usArray to inArray and .. to ....
This expression uses syntax that is not part of standard JavaScript:
:: (double colon) was proposed in early drafts as a bind operator but is not part of the official ECMAScript standard.
The combination concat::...inArray() is invalid in normal JavaScript engines and cannot be used as a valid way to flatten arrays.
In addition, inArray() would imply calling inArray as a function, but it is an array, which would cause a runtime error.
Hence, Option C is syntactically or semantically invalid in standard JavaScript and does not provide the required result [1, 2, 3, 4, 5].
Option D: [].concat.apply({}, inArray);
Corrected name from usArray to inArray.
Relevant concepts:
Again, Function.prototype.apply is used to call concat with a specific this value and arguments provided as an array.
concat treats its this value as an array-like object but ultimately returns a new array containing concatenated elements.
In this expression:
[].concat.apply({}, inArray);
thisArg is {} (an empty object).
argsArray is inArray, which is [ [1, 2], [3, 4, 5] ].
Using apply, this is interpreted as calling concat like:
Array.prototype.concat.call({}, [1, 2], [3, 4, 5]);
concat then:
Starts from the array-like this (here {} is treated as an empty base).
Takes the first argument [1, 2] and appends its elements 1 and 2 to the result.
Takes the second argument [3, 4, 5] and appends its elements 3, 4, and 5 to the result.
The resulting new array is:
[1, 2, 3, 4, 5]
Therefore, Option D also correctly produces the required array.
Final evaluation of all options:
Option A: Uses spread syntax with concat and correctly flattens one level: [1, 2, 3, 4, 5].
Option B: Equivalent to inArray.concat(), leaving it as [[1, 2], [3, 4, 5]]. Does not flatten the structure.
Option C: Uses non-standard and invalid syntax; not a valid or correct JavaScript solution.
Option D: Uses apply with concat, passing the inner arrays as arguments and flattening one level to [1, 2, 3, 4, 5].
Thus, the two correct statements are:
Reference of JavaScript knowledge documents or Study Guide (concept names only):
Array.prototype.concat (concatenating and one-level flattening behavior)
Spread syntax for arrays (...array)
Function.prototype.apply (calling a function with a given this value and arguments list)
Array flattening by one level using concat with array arguments
Distinction between nested arrays and flat arrays in JavaScript
Get access to all 147 verified questions with detailed answers.
Unlock All JS-Dev-101 Questions