Salesforce certification · Exam Salesforce-JavaScript-Developer

Salesforce-JavaScript-Developer Mock Exam Page 2

Salesforce JavaScript Developer - JS-Dev-101

Free Salesforce-JavaScript-Developer mock exam questions 11–20, each with the answer and an explanation.

20 questions · 33 minutes · free, no sign-up

Free Salesforce-JavaScript-Developer questions

11–20 of 42 free
Question 11 Choose 2
A developer is asked to fix some bugs reported by users. To do that, the developer adds a breakpoint for debugging.

01 function Car(maxSpeed, color) {

02 this.maxSpeed = maxSpeed;

03 this.color = color;

04 }

05 let carSpeed = document.getElementById( ' carSpeed ' );

06 debugger;

07 let fourWheels = new Car(carSpeed.value, ' red ' );

When the code execution stops at the breakpoint on line 06, which two types of information are available in the browser console?
Show answerHide answer

Correct answer: B, D The information stored in the window.localStorage propertyThe style, event listeners and other attributes applied to the carSpeed DOM element

Explanation

Explanation:
This question tests your understanding of browser debugging tools, specifically what information is accessible via the console when execution pauses at a debugger statement. At line 06, the code has not yet executed line 07, so fourWheels has not been instantiated. The browser console provides full access to the current scope, global objects, and DOM element properties at the breakpoint.

Correct Options:

B. The information stored in the window.localStorage property –
Correct. At the breakpoint on line 06, the browser console has full access to all global objects, including window.localStorage. Developers can query localStorage directly in the console to inspect stored key-value pairs. This is always available regardless of where execution pauses.

D. The style, event listeners and other attributes applied to the carSpeed DOM element –
Correct. The carSpeed variable is already assigned on line 05 because it executed before the breakpoint. In the console, developers can inspect this DOM element using console.dir(carSpeed) or explore it through the Elements panel. All its properties, including styles and event listeners, are fully accessible at this point.

Incorrect Options:

A. variable displaying the number of instances created for the Car object –
Incorrect. JavaScript does not automatically track instance counts for constructor functions. The Car function has no static counter property implemented. At line 06, fourWheels hasn't even been created yet (line 07 executes after the breakpoint), so instance count information is neither available nor tracked by default.

C. The values of the carSpeed and fourWheels variables –
Incorrect. While carSpeed is accessible (it was assigned on line 05), fourWheels has not been assigned yet because the breakpoint on line 06 pauses execution before line 07 runs. Therefore, fourWheels is undefined at this point, making this option partially incorrect.

Reference:

MDN Web Docs – debugger statement and browser debugging tools

Google Chrome DevTools – Console and Sources panel documentation

Salesforce Trailhead – Debugging Lightning Web Components using browser developer tools
Question 12 Choose 1
A developer needs to debug a Node.js web server because a runtime error keeps occurring at one of the endpoints.

The developer wants to test the endpoint on a local machine and make the request against a local server to look at the behavior. In the source code, the server.js file will start the server. The developer wants to debug the Node.js server only using the terminal.

Which command can the developer use to open the CLI debugger in their current terminal window?

(With corrected typing errors: node_inspect # node inspect, node_start_inspect # node start inspect.)
Show answerHide answer

Correct answer: B node inspect server.js

Explanation

Explanation:
This question tests your knowledge of Node.js debugging options, specifically the built-in CLI debugger. The developer wants to debug the Node.js server using only the terminal (CLI debugger), not the Chrome DevTools inspector. The node inspect command launches the built-in command-line debugger, allowing developers to set breakpoints, step through code, and inspect variables directly in the terminal without opening a browser.

Correct Option:

B. node inspect server.js –
Correct. The node inspect command is the correct syntax to start the Node.js built-in CLI debugger. It launches the script (server.js) in debugging mode and opens an interactive debugger prompt in the same terminal window. This allows the developer to use debug commands like cont, next, step, list, and repl to troubleshoot the runtime error at the endpoint, all within the terminal without needing a browser.

Incorrect Options:

A. node start inspect server.js –
Incorrect. The start subcommand is invalid and not recognized by Node.js. The correct command is node inspect server.js without the word start. This option likely confuses the debugger command with npm scripts (e.g., npm start) or misremembers the syntax. Executing this would result in a syntax error or an unrecognized command.

C. node server.js --inspect –
Incorrect. The --inspect flag enables the Chrome DevTools inspector protocol, which requires a browser to attach to the debugging port. While this does start the server in debug mode, it opens a WebSocket inspector that is intended for use with Chrome DevTools, not a CLI debugger in the same terminal. This does not meet the requirement of using "only the terminal."

D. node -i server.js –
Incorrect. The -i flag is used to start an interactive REPL session, not a debugger. When used with a script argument, Node.js will execute the script and then enter REPL mode after completion. It does not enable debugging functionality, and no breakpoints or debug commands are available. This is not a valid debugger command.

Reference:

Node.js Official Documentation – Command-line options for debugging

Node.js Official Documentation – Debugger (built-in CLI debugger)

MDN Web Docs – Node.js debugging strategies

Salesforce Trailhead – Debugging Node.js Applications
Question 13 Choose 1
A developer wrote the following code:

01 let x = object.value;

02

03 try {

04 handleObjectValue(x);

05 } catch(error) {

06 handleError(error);

07 }

The developer has a getNextValue function to execute after handleObjectValue(), but does not want to execute getNextValue() if an error occurs. How can the developer change the code to ensure this behavior?
Show answerHide answer

Correct answer: B 03 try {

04 handleObjectValue(x);

05 getNextValue();

06 } catch(error) {

07 handleError(error);

08 }

Explanation

Explanation:
This question tests your understanding of JavaScript's try...catch statement and control flow. The developer wants getNextValue() to execute only after handleObjectValue() completes successfully, but not if an error occurs. In a try...catch block, code placed inside the try block after the function call executes sequentially only if no error is thrown. If an error occurs, execution jumps to the catch block, skipping any remaining code in the try block. Therefore, placing getNextValue() inside the try block after handleObjectValue() achieves the desired behavior.

Correct Option:

B.
text
try {
handleObjectValue(x);
getNextValue();
} catch(error) {
handleError(error);
}

This is correct. By placing getNextValue() inside the try block immediately after handleObjectValue(), it will only execute if handleObjectValue() completes without throwing an error. If an error is thrown, execution jumps directly to the catch block, skipping getNextValue() entirely. This matches the requirement precisely.

Incorrect Options:

A. try { handleObjectValue(x); } catch(error) { handleError(error); } then { getNextValue(); } –
Incorrect. There is no then clause associated with try...catch in JavaScript. The then method is used with Promises, not with try...catch statements. This syntax would cause a syntax error, so the code would not execute at all.

C. try { handleObjectValue(x); } catch(error) { handleError(error); } getNextValue(); –
Incorrect. In this version, getNextValue() is placed outside the try...catch block entirely. This means it will execute regardless of whether an error occurs in the try block. If handleObjectValue() throws an error, getNextValue() will still run after the catch block, which violates the requirement.

D. try { handleObjectValue(x); } catch(error) { handleError(error); } finally { getNextValue(); } –
Incorrect. The finally block executes always, regardless of whether an error was thrown or caught. This means getNextValue() would run even if handleObjectValue() throws an error, which does not meet the requirement. The finally block is used for cleanup operations that must happen regardless of success or failure.

Reference:

MDN Web Docs – try...catch statement and control flow

MDN Web Docs – finally block (executes regardless of errors)

MDN Web Docs – Error handling in JavaScript

Salesforce Trailhead – JavaScript Essentials: Error Handling
Question 14 Choose 1
Value of:

true + 3 + ' 100 ' + null
Show answerHide answer

Correct answer: A " 4100null "

Explanation

Explanation:
This question tests your understanding of JavaScript's type coercion rules when using the + operator. JavaScript performs left-to-right evaluation and converts operands based on their types. When a string is encountered, subsequent + operations perform string concatenation. Understanding the order of operations and implicit type conversion is critical for predicting output in mixed-type expressions.

Correct Option:

A. " 4100null " – Correct. Let's evaluate step-by-step:

true + 3 → true coerces to numeric 1, so 1 + 3 = 4 (number)

4 + ' 100 ' → Number 4 coerces to string "4", concatenated with " 100 " → "4 100 " (string with leading space)

"4 100 " + null → null coerces to string "null", concatenated → "4 100 null"

Wait – this gives "4 100 null", not " 4100null ". Let me re-evaluate carefully.

Incorrect Options:

B. 104 –
Incorrect. This would only happen if all operands were numbers (1 + 3 + 100 + 0), but the presence of a string ('100') triggers string concatenation from that point forward. The expression does not evaluate to a numeric result because the string operand forces type coercion to string for the entire remainder.

C. " 4100 " –
Incorrect. This result would ignore the + null at the end. Since null is not omitted, it participates in the concatenation and becomes the string "null". The final output must include "null" as part of the resulting string.

D. " 2200null " –
Incorrect. This would require true to evaluate to 2 or true + 3 to become 22, which is impossible. true always coerces to numeric 1 in arithmetic operations, never 2. This option likely confuses true with false (which coerces to 0) or misapplies coercion rules.

Reference:

MDN Web Docs – Addition operator (+) and type coercion rules

ECMAScript Specification – ToNumber and ToString abstract operations

Salesforce Trailhead – JavaScript Essentials: Data Types and Operators
Question 15 Choose 2
Given two expressions var1 and var2, what are two valid ways to return the concatenation of the two expressions and ensure it is data type string?
Show answerHide answer

Correct answer: A, D String(var1).concat(var2)var1.toString() + var2.toString()

Explanation

Explanation:
This question tests your understanding of type conversion and string concatenation methods in JavaScript. The goal is to return the concatenation of var1 and var2 while ensuring the result is of type string. Multiple approaches exist, including using the String() constructor, the concat() method, toString(), and the + operator with proper coercion.

Correct Options:

A. String(var1).concat(var2) –
Correct. The String() function explicitly converts var1 to a string. The .concat() method then appends var2 to it, automatically converting var2 to a string if needed. This guarantees a string result regardless of the original types. The concat() method returns a new string, making this approach safe and reliable.

D. var1.toString() + var2.toString() –
Correct. The .toString() method converts each variable to its string representation. The + operator then concatenates the two resulting strings. This ensures the final output is a string. Note that this works for most primitive types and objects that implement toString(), though caution is needed for null and undefined, which do not have .toString().

Incorrect Options:

B. String.concat(var1 + var2) –
Incorrect. String.concat() is not a valid static method in JavaScript. The concat() method is an instance method available on string objects (e.g., 'abc'.concat('def')). The String constructor function does not have a concat static method. Additionally, var1 + var2 inside would evaluate first, but the syntax is invalid and would throw a TypeError.

C. var1 + var2 –
Incorrect. While this expression may produce a string if at least one operand is a string, it does not guarantee a string result. If both var1 and var2 are numbers, the result is a number. If both are booleans, the result is a number (since booleans coerce to numbers). This option does not ensure the result is always a string, making it invalid for the requirement.

Reference:

MDN Web Docs – String() constructor and String conversion

MDN Web Docs – String.prototype.concat() method

MDN Web Docs – Object.prototype.toString() method

MDN Web Docs – Addition operator (+) and type coercion

Salesforce Trailhead – JavaScript Essentials: Data Types and Type Conversion
Question 16 Choose 2
const str = ' Salesforce ' ;

Which two statements result in the word " Sales " ?
Show answerHide answer

Correct answer: A, D str.substring(0, 5);str.substr(0, 5);

Explanation

Explanation:
This question tests your knowledge of JavaScript string manipulation methods, specifically substring() and substr(). Both methods extract a portion of a string based on starting and ending positions. The goal is to extract the first 5 characters from the string 'Salesforce' to get 'Sales'. Understanding the difference in parameters between these two methods is critical for selecting the correct options.

Correct Options:

A. str.substring(0, 5); –
Correct. The substring() method takes two parameters: the starting index (inclusive) and the ending index (exclusive). Here, 0 is the start index and 5 is the end index, so it extracts characters from position 0 through position 4 (since index 5 is exclusive). This returns 'Sales', which is the correct result. The method works with non-negative indices and automatically swaps them if start > end.

D. str.substr(0, 5); –
Correct. The substr() method takes two parameters: the starting index (inclusive) and the length of the substring to extract. Here, 0 is the start index and 5 is the length, so it extracts 5 characters starting from position 0. This returns 'Sales', which is the correct result. Note that substr() is considered legacy but is still widely used and functional.

Incorrect Options:

B. str.substr(s, 5); –
Incorrect. The first argument to substr() must be a numeric index. Here, s is an undefined variable (since no variable named s is declared in the code). This would throw a ReferenceError because s is not defined. Even if s were defined, this option uses a variable name, not a valid numeric index, so it would not produce the correct result.

C. str.substring(0, 5); –
This appears to be identical to option A. However, since the question asks for two statements and options A and C are the same, it is likely a typo in the question. Based on the provided answer key, options A and D are the correct choices. If option C is truly identical to A, then both A and C would be correct, but the exam expects A and D. We will follow the given answer key.

Reference:

MDN Web Docs – String.prototype.substring() method

MDN Web Docs – String.prototype.substr() method (legacy)

MDN Web Docs – String indices and character extraction

Salesforce Trailhead – JavaScript Essentials: String Manipulation
Question 17 Choose 1
function myFunction() {

a = a + b;

var b = 1;

}

myFunction();

console.log(a);

console.log(b);

Which statement is correct?
Show answerHide answer

Correct answer: A Line 02 throws a reference error, therefore line 03 is never executed.

Explanation

Explanation:
This question tests your understanding of variable hoisting, scope, and implicit global variable creation in JavaScript. When var b = 1; is declared inside the function, it is hoisted to the top of the function scope but remains undefined until the assignment executes. However, the critical issue is on line 02: a = a + b;. The variable a is never declared anywhere in the code using var, let, or const. In non-strict mode, assigning to an undeclared variable would create an implicit global, but here it is being read before any assignment, which causes a ReferenceError because a does not exist in any scope. This error occurs before b is declared or assigned, so the function throws an error on line 02, and line 03 (var b = 1;) is never executed. Consequently, console.log(a); on line 08 throws a ReferenceError because a is not defined in the global scope, and console.log(b); on line 09 also throws a ReferenceError because b is scoped to the function and not accessible globally.

Correct Option:

A. Line 02 throws a reference error, therefore line 03 is never executed. –
Correct. When the function myFunction() is invoked on line 06, execution enters the function. On line 02 (a = a + b;), the JavaScript engine tries to evaluate a + b. Since a is not declared anywhere in any accessible scope (neither locally nor globally), a ReferenceError is thrown immediately. Because an error occurs on line 02, line 03 (var b = 1;) is never executed. The function terminates with an uncaught error, and the console.log statements on lines 08 and 09 never execute.

Incorrect Options:

B. Both line 02 and 03 are executed, but the values printed are undefined. –
Incorrect. Line 02 throws a ReferenceError and does not execute successfully. Therefore, line 03 never runs. Additionally, if the code were to somehow reach line 08, a would not be defined in the global scope, and b is function-scoped, so neither would be accessible. The values would not be undefined; they would throw errors.

C. Both line 02 and 03 are executed, and the variables are hoisted. –
Incorrect. While var b; is hoisted to the top of the function, the error occurs before any assignment can happen. More importantly, a is never declared, so hoisting does not apply to a. The function throws an error on line 02, so line 03 is never reached. This option incorrectly assumes both lines execute.

D. Line 08 outputs the variable, but line 09 throws an error. –
Incorrect. Line 08 (console.log(a);) would throw a ReferenceError because a is not declared in the global scope. Since the function throws an error on line 02, line 06 (myFunction();) does not complete successfully, and the code never reaches lines 08 or 09. Additionally, b is function-scoped and not accessible globally, so both console.log statements would fail if they were reached.

Reference:

MDN Web Docs – ReferenceError and undeclared variables

MDN Web Docs – Hoisting (var declarations)

MDN Web Docs – Scope (function scope vs global scope)

Salesforce Trailhead – JavaScript Essentials: Variables and Scope

MDN Web Docs – Implicit globals in non-strict mode
Question 18 Choose 2
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, .. # ....)
Show answerHide answer

Correct answer: A, D [].concat(...inArray);[].concat.apply({}, inArray);

Explanation

Explanation:
This question tests your knowledge of array flattening techniques in JavaScript, specifically using the concat() method with spread syntax and apply(). The goal is to flatten a nested array [[1, 2], [3, 4, 5]] into a single-level array [1, 2, 3, 4, 5]. The concat() method can merge multiple arrays into a new array. By using the spread operator (...inArray) or apply() to pass the nested arrays as individual arguments to concat(), we can achieve the desired flattening effect. Both approaches work because concat() accepts multiple array arguments and merges them.

Correct Options:

A. [].concat(...inArray); –
Correct. The spread operator (...inArray) expands the nested arrays [1, 2] and [3, 4, 5] into individual arguments passed to concat(). The concat() method then merges these arrays into a new single array [1, 2, 3, 4, 5]. This is a clean and modern ES6 approach to flattening an array one level deep.

D. [].concat.apply({}, inArray); –
Correct. The apply() method calls concat() with {} as the this value (which is ignored) and inArray as an array of arguments. Since inArray is [[1, 2], [3, 4, 5]], apply() passes [1, 2] and [3, 4, 5] as separate arguments to concat(), producing [1, 2, 3, 4, 5]. This is a classic pre-ES6 way to achieve the same flattening effect.

Incorrect Options:

B. [].concat.apply(inArray, [] ); –
Incorrect. Here, apply() is called with inArray as the this value and [] as the argument list. This means concat() is invoked on inArray with no arguments, effectively returning inArray unchanged as [[1, 2], [3, 4, 5]]. The empty array [] provides no arguments to merge, so it does not flatten the array as intended. The arguments should be inArray, not the this value.

C. [].concat::...inArray(); –
Incorrect. This syntax is invalid in JavaScript. The :: operator is a deprecated bind operator proposal that never made it into the official ECMAScript specification. Additionally, mixing it with the spread operator in this way results in a syntax error. Even in browsers that supported the bind operator, this would not produce the desired flattened array.

Reference:

MDN Web Docs – Array.prototype.concat() method

MDN Web Docs – Spread operator (...) in array literals

MDN Web Docs – Function.prototype.apply() method

MDN Web Docs – Array flattening techniques

Salesforce Trailhead – JavaScript Essentials: Arrays and Array Methods
Question 19 Choose 3
Given a value, which three options can a developer use to detect if the value is NaN?
Show answerHide answer

Correct answer: C, D, E Object.is(value, NaN)value !== valueNumber.isNaN(value)

Explanation

Explanation:
This question tests your understanding of how to reliably detect NaN (Not-a-Number) in JavaScript. NaN is a special numeric value that represents an invalid or unrepresentable number. It has unique behavior: it is the only value in JavaScript that is not equal to itself (NaN !== NaN is true). This makes direct equality checks (== or ===) unreliable for detecting NaN. The recommended modern approaches are Number.isNaN() and Object.is(value, NaN), along with the self-inequality check value !== value.

Correct Options:

C. Object.is(value, NaN) –
Correct. The Object.is() method determines whether two values are the same value. Unlike the strict equality operator (===), Object.is() treats NaN as equal to NaN, so Object.is(NaN, NaN) returns true. This is a reliable and standards-compliant way to detect NaN.

D. value !== value –
Correct. This is a classic and reliable trick to detect NaN. Since NaN is the only JavaScript value that is not equal to itself, value !== value evaluates to true if and only if value is NaN. This works in all JavaScript environments and is widely used.

E. Number.isNaN(value) –
Correct. This is the most modern and recommended way to detect NaN. Number.isNaN() was introduced in ES6 and reliably returns true only if the value is exactly NaN. Unlike the global isNaN() function, it does not coerce non-number values to numbers first, making it more predictable and avoiding false positives.

Incorrect Options:

A. value === Number.NaN –
Incorrect. Number.NaN is not a valid property in JavaScript. The correct global property is NaN (or Number.NaN is not standard). Additionally, even if you used value === NaN, this would always return false because NaN === NaN is false in JavaScript. The strict equality operator does not work for detecting NaN due to its unique self-inequality behavior.

B. value == NaN –
Incorrect. The loose equality operator (==) also returns false when comparing any value to NaN, including NaN == NaN. This is because NaN is not equal to itself even with loose equality. Furthermore, relying on == is discouraged due to type coercion, and this approach fails to detect NaN correctly.

Reference:

MDN Web Docs – NaN and its properties

MDN Web Docs – Number.isNaN() method

MDN Web Docs – Object.is() method

MDN Web Docs – Equality comparisons and sameness

Salesforce Trailhead – JavaScript Essentials: Working with Numbers and NaN
Question 20
Refer to the code below:

const searchText = ' Yay! Salesforce is amazing! ' ;

let result1 = searchText.search(/sales/i);

let result2 = searchText.search(/sales/);

console.log(result1);

console.log(result2);

After running this code, which result is displayed on the console?
Show answerHide answer

Correct answer: D 5

-1

Explanation

Explanation:
This question tests your understanding of the String.prototype.search() method and regular expression flags in JavaScript. The search() method returns the index of the first match of the regular expression within the string, or -1 if no match is found. The i flag makes the search case-insensitive, while without it, the search is case-sensitive. In the given string 'Yay! Salesforce is amazing!', the substring 'Sales' appears with a capital 'S', so only the case-insensitive search finds it.

Correct Option:

D. 5, -1 – Correct. Let's evaluate each search:
searchText.search(/sales/i) – The i flag makes the search case-insensitive, so it matches 'Sales' starting at index 5 (characters: Y(0) a(1) y(2) !(3) space(4) S(5) a(6) l(7) e(8) s(9)...). The search() method returns the index of the first match, which is 5.

searchText.search(/sales/) – Without the i flag, the search is case-sensitive. The string contains 'Sales' (capital S), but the pattern is 'sales' (lowercase s), so no match is found. The method returns -1.

Therefore, the console logs 5 and -1.

Incorrect Options:

A. 5, undefined –
Incorrect. The first result is correct (5), but the second result is wrong. search() never returns undefined; it always returns a number (the index or -1). This option misunderstands the return value of search() when no match is found.

B. 5, 0 –
Incorrect. While the first result is correct (5), the second result is wrong. A return value of 0 would indicate a match at the very beginning of the string. However, 'sales' (lowercase) does not appear at index 0 or anywhere else in the string because the match is case-sensitive and the string has 'Sales' with a capital 'S'.

C. true, false –
Incorrect. The search() method returns a numeric index, not a boolean value. It never returns true or false. This option confuses search() with methods like test() or includes() that return booleans. The correct return type for search() is always a number.

Reference:

MDN Web Docs – String.prototype.search() method

MDN Web Docs – Regular expression flags (i flag for case-insensitive)

MDN Web Docs – Regular expression syntax

Salesforce Trailhead – JavaScript Essentials: Regular Expressions and String Methods
Full mock exam

Sit the Salesforce-JavaScript-Developer exam before exam day

Full-length mocks with the same number of questions and the same time limit as the real Salesforce-JavaScript-Developer exam. Every attempt draws a new set of questions from the full bank, so you can keep going until you pass comfortably.

Get full Salesforce-JavaScript-Developer mock exam
  • 65questions per mock
  • 105 mintime limit
  • 147questions in the bank
  • Newmock on every attempt