Salesforce certification · Exam Salesforce-JavaScript-Developer

Salesforce-JavaScript-Developer Mock Exam

Salesforce JavaScript Developer - JS-Dev-101

Free Salesforce-JavaScript-Developer questions on Server Side JavaScript, each with the answer and an explanation.

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

Server Side JavaScript questions

1–5 of 5 free
Question 1 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 2 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 3 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 4 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 5 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

That's the last free question in Server Side JavaScript

You've seen the free 30% (5 of 17 questions). Full access unlocks every question and full-length Salesforce-JavaScript-Developer mock exams (65 questions, 105 minutes), a different mock on every attempt.

Unlock full mock exam
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