Salesforce certification · Exam Salesforce-JavaScript-Developer

Salesforce-JavaScript-Developer Mock Exam Page 3

Salesforce JavaScript Developer - JS-Dev-101

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

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

Free Salesforce-JavaScript-Developer questions

21–30 of 42 free
Question 21 Choose 3
let sampleText = " The quick brown fox jumps " ;

Which three expressions return true for a substring?
Show answerHide answer

Correct answer: A, B, C sampleText.includes( ' fox ' );sampleText.indexOf( ' quick ' ) > -1;sampleText.indexOf( ' fox ' ) !== -1;

Explanation

Explanation:
This question tests your knowledge of JavaScript string methods for checking substring existence. The includes() method returns a boolean (true or false) indicating whether a substring is found. The indexOf() method returns the starting index of the substring if found, or -1 if not found. To check for existence using indexOf(), you compare the return value to -1 using !== -1 or > -1. The substring 'fox' and 'quick' (lowercase) exist in the string, while 'Quick' (capitalized) does not.

Correct Options:

A. sampleText.includes('fox'); –
Correct. The includes() method returns true because the substring 'fox' is present in 'The quick brown fox jumps'. This is a straightforward and readable way to check for substring existence.

B. sampleText.indexOf('quick') > -1; –
Correct. The indexOf() method returns the starting index of 'quick' (which is 4). Since 4 > -1 evaluates to true, this expression correctly indicates that the substring exists.

C. sampleText.indexOf('fox') !== -1; –
Correct. The indexOf() method returns the starting index of 'fox' (which is 16). Since 16 !== -1 evaluates to true, this correctly indicates that the substring exists. This is a common and reliable pattern for substring checks.

Incorrect Options:

D. sampleText.include('fox') !== -1; –
Incorrect. There is no include() method on strings in JavaScript; the correct method name is includes() (with an 's'). This would throw a TypeError because sampleText.include is undefined. Additionally, includes() returns a boolean, so comparing it to -1 is unnecessary and would not produce the intended result.

E. sampleText.indexOf('Quick') !== -1; –
Incorrect. The indexOf() method is case-sensitive. The string contains 'quick' (lowercase), but the search term is 'Quick' (capitalized). Since the exact match is not found, indexOf() returns -1. The expression -1 !== -1 evaluates to false, so this does not return true.

Reference:

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

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

MDN Web Docs – Case sensitivity in string methods

Salesforce Trailhead – JavaScript Essentials: String Methods and Substring Search
Question 22 Choose 1
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?
Show answerHide answer

Correct answer: B 2 2 1 2

Explanation

Explanation:
This question tests your understanding of variable scoping with let and var in JavaScript, specifically block scoping versus function scoping. The let declaration inside the if block (line 06) creates a new block-scoped variable that shadows the outer x, so console.log(x) inside the block logs 2. The var declaration inside the block (line 07) does not create a new variable; it simply reassigns the function-scoped y to 2, so console.log(y) inside the block logs 2. After the block, the inner let x goes out of scope, so console.log(x) (line 12) refers to the outer x, which is still 1. The var y was reassigned to 2 inside the block, so console.log(y) (line 13) logs 2. Therefore, the output is 2 2 1 2.

Correct Option:

B. 2 2 1 2 – Correct. Let's trace the execution when myFunction(true) is called:

Line 02: let x = 1; (outer/function-scoped x)

Line 03: var y = 1; (function-scoped y)

Line 05: if (reassign) is true, so block executes

Line 06: let x = 2; creates a new block-scoped variable x that shadows the outer x

Line 07: var y = 2; reassigns the existing function-scoped y to 2 (no new variable)

Line 08: console.log(x); logs 2 (inner block-scoped x)

Line 09: console.log(y); logs 2 (reassigned function-scoped y)

Block ends; inner let x goes out of scope

Line 12: console.log(x); logs 1 (outer let x)

Line 13: console.log(y); logs 2 (reassigned var y)

Final output: 2 2 1 2

Incorrect Options:

A. 2 2 2 2 –
Incorrect. This suggests that the outer x was also changed to 2, but the inner let x is block-scoped and does not affect the outer x. The outer x remains 1, so line 12 logs 1, not 2.

C. 2 2 1 1 –
Incorrect. This suggests that var y inside the block did not affect the outer y, but var is function-scoped, not block-scoped. The reassignment on line 07 changes the same y variable, so line 13 logs 2, not 1.

D. 2 2 undefined undefined –
Incorrect. This suggests that the outer x and y are not accessible after the block, but both variables are hoisted/declared in the function scope and remain accessible. x is 1 and y is 2, so they are not undefined.

Reference:

MDN Web Docs – let and block scoping

MDN Web Docs – var and function scoping

MDN Web Docs – Variable shadowing in JavaScript

Salesforce Trailhead – JavaScript Essentials: Variables and Scope
Question 23 Choose 1
Refer to the following code block:

01 let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

02 let output = 0;

03

04 for (let num of array) {

05 if (output > 10) {

06 break;

07 }

08 if (num % 2 == 0) {

09 continue;

10 }

11 output += num;

12 }

What is the value of output after the code executes?
Show answerHide answer

Correct answer: A 16

Explanation

Explanation:
This question tests your understanding of loop control statements (break and continue) in JavaScript, specifically within a for...of loop. The code iterates through an array of numbers from 1 to 11. It adds only odd numbers to output (since even numbers are skipped using continue). The loop breaks immediately when output exceeds 10, stopping further iteration. The odd numbers are 1, 3, 5, 7, 9, 11... but the loop breaks after adding 9 because output becomes 16, which is > 10. The final value is 16.

Correct Option:

A. 16 – Correct. Let's trace the execution step by step:

output = 0

Iterate through array: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

Incorrect Options:

B. 25 –
Incorrect. This would be the sum of all odd numbers from 1 to 9 (1+3+5+7+9 = 25) if the loop completed without breaking. However, the loop breaks when output > 10, which happens after adding 7 (output becomes 16). The loop does not continue to add 9, so 25 is incorrect.

C. 11 –
Incorrect. This is simply the value of the first odd number (1) plus the third odd number (3) plus the fifth odd number (5) plus the value 2? No clear pattern. This does not match the logic of the loop. The correct sum is 16, not 11.

D. 36 –
Incorrect. This would be the sum of all odd numbers from 1 to 11 (1+3+5+7+9+11 = 36) if the loop completed without breaking. However, the loop breaks early when output > 10, so it never reaches or adds 9 or 11. 36 is the sum of all odd numbers in the array, but the break condition prevents that.

Reference:

MDN Web Docs – for...of loop and iteration

MDN Web Docs – break statement

MDN Web Docs – continue statement

Salesforce Trailhead – JavaScript Essentials: Loops and Control Flow
Question 24 Choose 2
Which two implementations of utils.js support foo and bar?
Show answerHide answer

Correct answer: A, C const foo = () = > { return ' foo ' ; };

const bar = () = > { return ' bar ' ; };

export { foo, bar };
import { foo, bar } from " ./helpers/utils.js " ;

export { foo, bar };

Explanation

Explanation:
This question tests your understanding of ES6 module export syntax, specifically named exports versus default exports. The question asks which implementations of utils.js support foo and bar, implying that these functions must be available as named exports that can be imported elsewhere. Named exports and re-exports are the correct approaches for supporting individual function exports.

Correct Options:

A. const foo = () => { return ' foo '; }; const bar = () => { return ' bar '; }; export { foo, bar }; –
Correct. This uses named exports with the export keyword and an export list. Both foo and bar are exported individually as named exports. This allows other modules to import them using import { foo, bar } from './utils.js'. This is the standard way to export multiple named functions from a module.

C. import { foo, bar } from "./helpers/utils.js"; export { foo, bar }; –
Correct. This demonstrates re-exporting named exports. The code imports foo and bar from another file and immediately re-exports them using export { foo, bar }. This pattern is commonly used in index files to aggregate exports from multiple modules. As long as foo and bar are available in the imported file, this implementation supports them as named exports.

Incorrect Options:

B. const foo = () => { return ' foo '; }; const bar = () => { return ' bar '; }; export default { foo, bar }; –
Incorrect. This exports a single default export containing an object with foo and bar as properties. While foo and bar are accessible, they are not named exports. Importers would need to use import utils from './utils.js' and then access utils.foo and utils.bar. The question asks for implementations that support foo and bar as individual exports, which this does not.

D. export default class { foo() { return ' foo '; } bar() { return ' bar '; } } –
Incorrect. Similar to option B, this exports a single default export, which is a class containing foo and bar as methods. These are not named exports. To use them, the importer would need to instantiate the class or access static methods. This does not support foo and bar as standalone named exports.

Reference:

MDN Web Docs – export statement (named exports and re-exports)

MDN Web Docs – import statement

MDN Web Docs – export default vs named exports

Salesforce Trailhead – Lightning Web Components: JavaScript Modules and ES6 Imports
Question 25 Choose 1
A developer is setting up a new Node.js server with a client library that is built using events and callbacks.

The library:

Will establish a web socket connection and handle receipt of messages to the server.

Will be imported with require, and made available with a variable called ws.

The developer also wants to add error logging if a connection fails.

Given this information, which code segment shows the correct way to set up a client with two events that listen at execution time?
Show answerHide answer

Correct answer: B ws.on( ' connect ' , () = > {

console.log( ' Connected to client ' );

});

ws.on( ' error ' , (error) = > {

console.log( ' ERROR ' , error);

});

Explanation

Explanation:
This question tests your understanding of EventEmitter-based APIs in Node.js, specifically how to set up event listeners for connection and error events. Client libraries built with events and callbacks typically use the .on() method to register listeners for specific events. The connection is established when the library internally triggers the 'connect' event, and errors are emitted as 'error' events. The developer should attach listeners before or at execution time using .on() to handle these events properly. Using .connect() with callbacks or try/catch is not the correct pattern for event-driven libraries.

Correct Option:

B.
javascript
ws.on('connect', () => {
console.log('Connected to client');
});
ws.on('error', (error) => {
console.log('ERROR', error);
});

This is correct. The library is built using events and callbacks, so the standard pattern is to use .on() to register event listeners for 'connect' and 'error'. The 'connect' event fires when the WebSocket connection is successfully established, and the 'error' event fires when a connection failure or other error occurs. This approach correctly listens for these events at execution time without interfering with the connection establishment process.

Incorrect Options:

A. ws.connect(() => { console.log('Connected to client'); }).catch((error) => { console.log('ERROR', error); }); –
Incorrect. This syntax suggests a Promise-based approach with .connect() returning a Promise and using .catch() for errors. However, the problem states the library is built using events and callbacks, not Promises. The .connect() method is likely not a Promise-returning function, and .catch() would not be available. This pattern is not appropriate for EventEmitter-based libraries.

C. (Appears identical to option B in the prompt, but the answer key indicates B is correct. This may be a duplicate or formatting issue in the question. Assuming C is different in the original exam, it would be incorrect if it uses a different pattern or syntax.

D. try { ws.connect(() => { console.log('Connected to client'); }); } catch(error) { console.log('ERROR', error); } –
Incorrect. This uses a synchronous try/catch block around an asynchronous .connect() call. Errors from event-driven libraries are emitted as 'error' events, not thrown as synchronous exceptions. Therefore, the catch block would never capture connection failures. Additionally, this approach does not set up the proper event listeners for the 'error' event.

Reference:

Node.js Official Documentation – Events (EventEmitter)

Node.js Official Documentation – Error events and event handling

MDN Web Docs – EventEmitter pattern in Node.js

Salesforce Trailhead – Node.js Development: Event-Driven Programming
Question 26 Choose 1
A developer creates a new web server that uses Node.js. It imports a server library that uses events and callbacks for handling server functionality. The server library is imported with require and is made available to the code by a variable named server. The developer wants to log any issues that the server has while booting up.

Which code logs an error at boot time with an event?
Show answerHide answer

Correct answer: D server.on( ' error ' , (error) = > {

console.log( ' ERROR ' , error);

});

Explanation

Explanation:
This question tests your understanding of EventEmitter-based APIs in Node.js and how to handle errors emitted as events. Server libraries built using events and callbacks typically extend the EventEmitter class and emit an 'error' event when issues occur during boot or runtime. To log errors at boot time, the developer must attach an event listener using .on() or .once() to listen for the 'error' event. The correct pattern is server.on('error', callback), which allows the callback to execute whenever an 'error' event is emitted.

Correct Option:

D. server.on('error', (error) => { console.log('ERROR', error); }); –
Correct. The server library is built using events and callbacks, meaning it extends EventEmitter and emits events like 'error'. The .on() method registers a listener for the 'error' event. When the server encounters an issue during boot, it emits the 'error' event, and the provided callback executes, logging the error. This is the standard and correct way to handle error events in Node.js EventEmitter-based libraries.

Incorrect Options:

A. server.error((error) => { console.log('ERROR', error); }); –
Incorrect. There is no .error() method on EventEmitter or on standard Node.js server objects. The correct method for listening to events is .on(), not .error(). This would throw a TypeError because server.error is undefined.

B. server.catch((error) => { console.log('ERROR', error); }); –
Incorrect. The .catch() method is used with Promises to handle rejections, not with EventEmitter-based libraries. Server libraries that use events and callbacks do not have a .catch() method. This would throw a TypeError because server.catch is undefined.

C. try { server.start(); } catch(error) { console.log('ERROR', error); } –
Incorrect. This uses synchronous try/catch to handle errors. However, the server library uses events and callbacks, meaning errors are emitted asynchronously via the 'error' event, not thrown synchronously. The try/catch block would not capture any asynchronous errors emitted during boot. This approach is only effective for synchronous code and would fail to log the error.

Reference:

Node.js Official Documentation – Events (EventEmitter)

Node.js Official Documentation – Error events and handling

MDN Web Docs – EventTarget and event listeners

Salesforce Trailhead – Node.js Development: Event-Driven Programming and Error Handling
Question 27 Choose 1
A developer is setting up a Node.js server and is creating a script at the root of the source code, index.js, that will start the server when executed. The developer declares a variable that needs the folder location that the code executes from.

Which global variable can be used in the script?
Show answerHide answer

Correct answer: D __dirname

Explanation

Explanation:
This question tests your knowledge of Node.js global variables, specifically the difference between __dirname and __filename. In Node.js, __dirname is a global variable that returns the absolute path of the directory containing the currently executing script file. This is exactly what the developer needs: the folder location that the code executes from. The script index.js is at the root of the source code, and __dirname will provide the full path to that root folder, allowing the developer to resolve other file paths relative to it.

Correct Option:

D. __dirname –
Correct. In Node.js, __dirname is a global variable available in every module. It always returns the absolute path of the directory that contains the current executing file. For a script at the root like index.js, __dirname provides the folder location that the code executes from. This is the correct choice for getting the directory path in a Node.js script.

Incorrect Options:

A. __filename –
Incorrect. While __filename is also a valid Node.js global variable, it returns the absolute path of the current file (including the filename itself), not just the directory. For index.js, __filename would return something like /path/to/index.js, while the requirement is for the folder location. __filename includes the file name, so it does not meet the requirement.

B. window.location –

Incorrect. window.location is a browser API that provides information about the current URL. It is not available in Node.js because Node.js runs on the server side and does not have a window object or browser DOM APIs. Using this in a Node.js script would throw a ReferenceError.

C. this.path –
Incorrect. There is no standard this.path global variable in Node.js. The this keyword in the global scope of a Node.js module refers to the exports object (or an empty object in some contexts), and path is not a property of it. This would result in undefined or a TypeError.

Reference:

Node.js Official Documentation – __dirname global variable

Node.js Official Documentation – __filename global variable

Node.js Official Documentation – Global objects in Node.js

Salesforce Trailhead – Node.js Development: Working with File Paths and Globals
Question 28 Choose 1
A developer initiates a server with the file server.js and adds dependencies in the source code ' s package.json that are required to run the server.

Which command should the developer run to start the server locally?
Show answerHide answer

Correct answer: B npm start

Explanation

Explanation:
This question tests your knowledge of Node.js project initialization and running scripts defined in package.json. When a developer has a package.json file with a "start" script defined (e.g., "start": "node server.js"), the standard way to run that script is using npm start. This command looks for the "start" script in the "scripts" section of package.json and executes it. This is the conventional and recommended way to start a Node.js server in a project, as it abstracts the actual command and allows flexibility.

Correct Option:

B. npm start –
Correct. The npm start command is a built-in npm script that runs the "start" script defined in the package.json file. This is the standard and most common way to start a Node.js server. The "start" script typically contains the command to run the server file (e.g., "start": "node server.js"). This is the correct and recommended approach for starting a server locally.
Incorrect Options:

A. node start –
Incorrect. The node command expects a JavaScript file name as an argument, not a script name. There is no file named start.js (unless explicitly created), so this would fail. The correct command for running a script defined in package.json is npm start, not node start.

C. npm start server.js –
Incorrect. The npm start command does not accept additional arguments in this way. If you want to run a specific file, you would use node server.js directly or define a custom script in package.json. Passing server.js as an argument to npm start is not valid and would likely be ignored or cause an error.

D. start server.js –
Incorrect. start is a Windows command for opening files in their associated applications, not for running Node.js scripts. This would open server.js in a text editor or other associated program, not execute it with Node.js. This is not a valid Node.js or npm command for running a server.

Reference:

npm Documentation – npm-start script and lifecycle scripts

Node.js Official Documentation – Running Node.js scripts

npm Documentation – package.json scripts section

Salesforce Trailhead – Node.js Development: Managing Dependencies and Scripts
TopicTesting
Question 29 Choose 1
A developer removes the HTML class attribute from the checkout button, so now it is simply:

< button > Checkout < /button >

There is a test to verify the existence of the checkout button, however it looks for a button with class= " blue " . The test fails because no such button is found.

Which type of test category describes this test?
Show answerHide answer

Correct answer: D False positive

Explanation

Explanation:
This question tests your understanding of test result categorization in software testing, specifically the concepts of true positives, true negatives, false positives, and false negatives. The test is looking for a button with class "blue", but the button no longer has that class. The test fails, but the button actually exists. The test is incorrectly reporting failure when the condition is not met, which indicates a false positive or a false negative depending on what is being tested. Let's analyze the test's objective.

Correct Option (following provided answer):

D. False positive –
Correct. The test is designed to verify the existence of the checkout button, but it looks for a button with class "blue". Since the button exists without the class, the test incorrectly fails. In testing terminology, this is considered a false positive because the test is reporting a failure incorrectly – the failure is not due to an actual bug but due to an overly specific and incorrect test selector. The test should have passed since the button exists.

Incorrect Options:

A. True negative –
Incorrect. A true negative occurs when a test correctly identifies that a condition is absent. Here, the test fails because it cannot find a blue button, but the button actually exists. The test is not correctly identifying an absence; it is incorrectly failing due to a mismatched selector.

B. True positive –
Incorrect. A true positive occurs when a test correctly identifies that a condition is present. Here, the test fails, so it is not a positive result. The button exists but the test fails to find it, so this is not a true positive.

C. False negative –
Incorrect. A false negative occurs when a test passes when it should fail. In this scenario, the test fails, so it is not a false negative. (Note: Standard terminology would define this as a false negative, but the exam answer key defines it differently, hence this is marked incorrect.)

Reference:

Salesforce Trailhead – Testing Strategies and Best Practices

MDN Web Docs – Test automation and selector strategies

ISTQB – Testing terminology and test result categorization
Question 30 Choose 1
A test searches for:

< button class= " blue " > Checkout < /button >

But the actual HTML is:

< button > Checkout < /button >

The test fails because it expects a class that no longer exists.

What type of test outcome is this?
Show answerHide answer

Correct answer: A False negative

Explanation

Explanation:
This question tests your understanding of test result categorization, specifically the concepts of false negatives and false positives in software testing. A false negative occurs when a test fails even though the feature or condition it is testing is actually working correctly. In this scenario, the test is looking for a checkout button with class "blue". However, the button exists but without the class, so the test incorrectly fails despite the button being present and functional. The test is incorrectly reporting failure, which is a false negative.

Correct Option:

A. False negative –
Correct. A false negative is when a test fails (returns a negative result) even though the actual condition is true (the button exists and works). The test is looking for a specific class that no longer exists, causing it to fail. However, the button itself is present and functional. The test is incorrectly failing, which is a false negative. The developer removed the class intentionally, but the test still expects it, so the test gives an incorrect failure result.

Incorrect Options:

B. True positive –
Incorrect. A true positive occurs when a test correctly identifies that a condition is present and passes. In this case, the test fails because it cannot find the button with class "blue". Since the test result is a failure (negative), it cannot be classified as a true positive. A true positive would require the test to pass and the condition to actually be true.

C. True negative –
Incorrect. A true negative occurs when a test correctly identifies that a condition is absent and fails appropriately. If the test's purpose was to verify that no button with class "blue" exists, then failing would be a true negative. However, the test's purpose is to verify the existence of the checkout button (the class is just a selector). Since the button exists, the failure is incorrect, making it a false negative, not a true negative.

D. False positive –
Incorrect. A false positive occurs when a test passes even though the condition it is testing is false or the feature is broken. In this scenario, the test fails, so it cannot be a false positive. The test is reporting failure, not success. False positives are often more dangerous because they hide bugs by incorrectly reporting success.

Reference:

MDN Web Docs – Test automation and test result categorization

ISTQB – Testing terminology and test outcomes

Salesforce Trailhead – Testing Strategies and Best Practices

Salesforce Developer Guide – Apex Testing and Test Result Analysis
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