Free Salesforce JavaScript Developer Practice Test Questions
Topic: Server Side JavaScript
147 questionsUpdated 23-Nov-20267 topics
TopicServer Side JavaScript
Question 1Choose 2
Which two implementations of utils.js support foo and bar?
Correct answer:
A. const foo = () = > { return ' foo ' ; };
const bar = () = > { return ' bar ' ; };
export { foo, bar };
C. import { foo, bar } from " ./helpers/utils.js " ;
export { foo, bar };
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 2Choose 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?
Correct answer:
B. ws.on( ' connect ' , () = > {
console.log( ' Connected to client ' );
});
ws.on( ' error ' , (error) = > {
console.log( ' ERROR ' , error);
});
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
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?
Correct answer:
D. server.on( ' error ' , (error) = > {
console.log( ' ERROR ' , error);
});
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 4Choose 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?
Correct answer:
D. __dirname
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
That's the last free question in Server Side JavaScript
You've seen the free 25% of this topic (4 of 17 questions). Get complete access for every question, explanation and the timed exam simulator.
Our timed 2026 Salesforce JavaScript Developer practice test mirrors the format, number of questions and time limit of the official exam.
The #1 challenge isn't just knowing the material, it's managing the clock. The simulation builds your speed and stamina, so you walk in knowing exactly what to expect.