Total 221 Questions
Last Updated On : 11-Sep-2025 - Spring 25 release
Preparing with Salesforce-JavaScript-Developer practice test is essential to ensure success on the exam. This Salesforce SP25 test allows you to familiarize yourself with the Salesforce-JavaScript-Developer exam questions format and identify your strengths and weaknesses. By practicing thoroughly, you can maximize your chances of passing the Salesforce certification spring 2025 release exam on your first attempt. Surveys from different platforms and user-reported pass rates suggest Salesforce-JavaScript-Developer practice exam users are ~30-40% more likely to pass.
A developer is creating a simple webpage with a button. When a user clicks this button for the first time, a message is displayed.
The developer wrote the JavaScript code below, but something is missing. The message gets displayed every time a user clicks the button, instead of just the first time.
01 function listen(event) {
02 alert ( ‘Hey! I am John Doe’) ;03 button.addEventListener (‘click’, listen);
Which two code lines make this code work as required?
(Choose 2 answers)
A. On line 02, use event.first to test if it is the first execution.
B. On line 04, use event.stopPropagation ( ),
C. On line 04, use button.removeEventListener(‘ click” , listen);
D. On line 06, add an option called once to button.addEventListener().
Explanation:
The problem is that the event listener keeps firing on every click. To make it run only the first time, we need a way to stop it after the first execution. There are two valid approaches in JavaScript:
Manually remove the event listener
Inside the handler (listen), after showing the alert, we can call:
button.removeEventListener('click', listen);
This detaches the listener so it won’t trigger again.
Use the once option in addEventListener
Modern JavaScript allows passing an options object to addEventListener:
button.addEventListener('click', listen, { once: true });
With once: true, the listener is automatically removed after the first execution.
❌ Why the other options are incorrect
A. On line 02, use event.first to test if it is the first execution.
→ There’s no such property as event.first in JavaScript events. Invalid.
B. On line 04, use event.stopPropagation().
→ stopPropagation() prevents the event from bubbling up the DOM, but it doesn’t stop the handler from being executed again on the next click.
📚 Reference:
MDN Web Docs – addEventListener()
MDN Web Docs – removeEventListener()
Refer to the following code:
Let sampleText = ‘The quick brown fox jumps’;
A developer needs to determine if a certain substring is part of a string. Which three expressions return true for the given substring ?
(Choose 3 answers)
A. sampleText.includes(‘fox’);
B. sampleText.includes(‘ quick ’, 4);
C. sampleText.includes(‘ Fox ’, 3)
D. sampleText.includes(‘ fox ’);
E. sampleText.includes(‘ quick ’) !== -1;
Explanation:
To determine which three expressions return true for checking if a substring is part of the string sampleText = 'The quick brown fox jumps', let’s analyze each option using the JavaScript includes() method, which checks if a string contains a specified substring and returns a boolean (true or false). The includes() method is case-sensitive and can take an optional second parameter to specify the starting index for the search.
Analysis of Each Option
A. sampleText.includes('fox')
The includes() method checks if 'fox' is a substring of sampleText.
The string 'fox' appears in sampleText at position 16-18 ('The quick brown fox jumps').
Since 'fox' is present and the search is case-sensitive (matches exactly), this returns true.
Result: true
B. sampleText.includes(' quick ', 4)
This checks if ' quick ' (with spaces before and after) exists in sampleText, starting the search from index 4.
The substring ' quick ' appears in sampleText at indices 3-9 ('The quick brown fox jumps').
Starting from index 4, the search includes the range where ' quick ' exists (indices 4-9 are part of the substring).
Since the substring is found, this returns true.
Result: true
C. sampleText.includes(' Fox ', 3)
This checks if ' Fox ' (with a capital 'F' and spaces before and after) exists in sampleText, starting from index 3.
The string 'fox' in sampleText has a lowercase 'f', and includes() is case-sensitive, so ' Fox ' does not match.
Additionally, there’s no substring ' Fox ' (with spaces and capital 'F') in sampleText.
This returns false.
Result: false
D. sampleText.includes(' fox ')
This checks if ' fox ' (with spaces before and after) exists in sampleText.
In sampleText, 'fox' appears at indices 16-18, preceded by 'brown ' and followed by ' jumps'.
The exact substring ' fox ' (space, lowercase 'fox', space) matches the portion of sampleText from indices 15-19 ('brown fox jumps').
Since the substring is found, this returns true.
Result: true
E. sampleText.includes(' quick ') !== -1
This checks if ' quick ' (with spaces) exists in sampleText and then applies !== -1.
The includes() method returns a boolean (true or false), not a number like -1. For example, sampleText.includes(' quick ') returns true because ' quick ' exists at indices 3-9.
However, comparing true !== -1 evaluates to true because true (coerced to 1 in a loose comparison) is not equal to -1.
This expression is confusing because includes() doesn’t return -1 (unlike indexOf()), but due to JavaScript’s type coercion, true !== -1 yields true.
Result: true (but note this is a poorly constructed expression, likely a distractor).
Explanation Notes
The includes() method is case-sensitive, so ' Fox ' (option C) fails due to the capital 'F'.
Option E is technically correct due to JavaScript’s loose comparison, but it’s an unusual way to write the condition, as includes() already returns a boolean. It’s likely included to test understanding of JavaScript coercion or to confuse with indexOf().
References:
MDN: String.prototype.includes() confirms includes() returns a boolean and is case-sensitive.
Salesforce JavaScript Developer I exams often test standard JavaScript string methods and edge cases like case sensitivity or substring matching.
Given the code below:
Setcurrent URL ();
console.log(‘The current URL is: ‘ +url );
function setCurrentUrl() {
Url = window.location.href:
What happens when the code executes?
A. The url variable has local scope and line 02 throws an error.
B. The url variable has global scope and line 02 executes correctly.
C. The url variable has global scope and line 02 throws an error.
D. The url variable has local scope and line 02 executes correctly.
Explanation:
This question tests the understanding of variable scope, specifically the concept of implied globals and the difference between var, let, const, and undeclared variables.
Let's break down the code:
Line 01: setCurrentUrl(); - This calls the function setCurrentUrl.
Line 02: console.log('The current URL is: ' + url); - This attempts to log the value of a variable named url.
Line 03-05: The function setCurrentUrl is defined.
function setCurrentUrl() {
url = window.location.href;
}
The key is what happens inside the function on Line 04: url = window.location.href;
The variable url is not declared with var, let, or const inside the function.
In non-strict mode (which is likely the default context here), assigning a value to an undeclared variable implicitly creates a global variable.
Therefore, when setCurrentUrl() is executed on Line 01, it creates a global variable named url and assigns it the value of window.location.href.
Now, let's evaluate the options:
A. The url variable has local scope and line 02 throws an error. This is incorrect. Because the variable was not declared with var, let, or const inside the function, it does not have local scope. It becomes a global.
B. The url variable has global scope and line 02 executes correctly. This is correct. As explained, the assignment inside the function without a declaration creates a global variable. By the time Line 02 executes, the global variable url exists and holds a value, so the console.log statement works without throwing an error.
C. The url variable has global scope and line 02 throws an error. This is incorrect. While the variable does have global scope, Line 02 will not throw an error because the variable exists in the global scope when the line is executed.
D. The url variable has local scope and line 02 executes correctly. This is incorrect. The variable does not have local scope (it's global), and if it did have local scope (e.g., if it was declared with let url = ...), then Line 02 would indeed throw a ReferenceError because it would be trying to access a variable from a scope where it is not defined.
Reference:
MDN Web Docs on undeclared variables:
The concept of "implied globals" is a classic behavior of JavaScript's non-strict mode.
Conclusion:
The code creates a global variable, allowing the console.log statement to access it and execute successfully.
A developer is required to write a function that calculates the sum of elements in an array but is getting undefined every time the code is executed. The developer needs to find what is missing in the code below.
Const sumFunction = arr => {
Return arr.reduce((result, current) => {
//
Result += current;
//
), 10);
);
Which option makes the code work as expected?
A. Replace line 02 with return arr.map(( result, current) => (
B. Replace line 04 with result = result +current;
C. Replace line 03 with if(arr.length == 0 ) ( return 0; )
D. Replace line 05 with return result;
Explanation:
🧠 Problem Breakdown
Here’s the broken code (cleaned up for clarity):
const sumFunction = arr => {
return arr.reduce((result, current) => {
result += current;
// Missing return here!
}, 10);
};
The issue:
The callback inside reduce() is not returning the updated result.
In arrow functions with {}, you must explicitly return a value.
Without a return, reduce() passes undefined as the accumulator in the next iteration.
D. Replace line 05 with return result;
This fixes the function by ensuring the callback returns the updated accumulator:
const sumFunction = arr => {
return arr.reduce((result, current) => {
result += current;
return result;
}, 10);
};
❌ Why the other options fail:
A. Replace with map()❌
map() returns a new array, not a sum. Wrong method.
B. Use result = result + current; ❌
Still missing return result; — syntax is fine, logic still broken.
C. Add if(arr.length == 0) return 0; ❌
Defensive coding, but doesn’t fix the missing return inside reduce().
D. Add return result; ✅
Corrects the missing return inside the reduce() callback.
🔁 Bonus Tip:
You can simplify the function using an implicit return:
const sumFunction = arr => arr.reduce((result, current) => result + current, 10);
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. Given the code and the information the developer has, which code logs an error at boost with an event?
A. Server.catch ((server) => {
console.log(‘ERROR’, error);
});
B. Server.error ((server) => {
console.log(‘ERROR’, error);
});
C. Server.on (‘error’, (error) => {
console.log(‘ERROR’, error);
});
D. Try{
server.start();
} catch(error) {
console.log(‘ERROR’, error);
Explanation:
🔎 Key facts
In Node.js, objects that emit events (like http.Server) are instances of EventEmitter.
To listen for errors, you must register an error event listener:
server.on('error', (error) => {
console.error('ERROR', error);
});
catch only works with synchronous errors in a try...catch block. Asynchronous events (like server boot errors) must be handled with .on('error', …).
❌ Wrong options
A. server.catch(...)
No .catch() method on EventEmitter objects. That’s for Promises. ❌
B. server.error(...)
Not a valid method. Error events must be attached with .on('error', …). ❌
D. try { server.start(); } catch(error) {...}
Won’t catch async startup issues — only synchronous code inside start() itself. ❌
✅ Correct option
C. server.on('error', (error) => { console.log('ERROR', error); });
This is the correct way to handle startup or runtime errors emitted by a Node.js server.
📚 Reference:
Node.js – EventEmitter error event
Node.js HTTP Server error handling
Given the following code:
Let x =(‘15’ + 10)*2;
What is the value of a?
A. 3020
B. 1520
C. 50
D. 35
Explanation:
Code Analysis
let x = ('15' + 10) * 2;
'15' + 10: The + operator concatenates the string '15' with the number 10 (coerced to '10'), resulting in the string '1510'.
'1510' * 2: The * operator coerces the string '1510' to the number 1510, then multiplies by 2, yielding 3020.
x is assigned 3020 (a number).
The question asks for a, but the code defines x. Assuming a typo (common in exam questions), the value of x is 3020. No information is provided about a.
Options
A. 3020: Correct, matches the result.
B. 1520: Incorrect, would require numeric addition (15 + 10), not concatenation.
C. 50: Incorrect, assumes numeric addition 15 + 10 = 25, then 25 * 2.
D. 35: Incorrect, misinterprets the operation (e.g., 15 + 10 + 10).
Explanation Notes
JavaScript’s + operator performs string concatenation when one operand is a string.
The * operator coerces a numeric string to a number for multiplication.
The question likely meant x instead of a. If a is a separate variable, additional context is needed.
References:
MDN: Addition (+)
MDN: Type Coercion
Salesforce JavaScript Developer I exams test type coercion and operator behavior.
A developer creates an object where its properties should be immutable and prevent properties from being added or modified. Which method should be used to execute this business requirement?
A. Object.const()
B. Object.eval()
C. Object.lock()
D. Object.freeze()
Explanation:
This question tests the knowledge of JavaScript methods used to control the mutability of objects. The requirement is to make an object's properties immutable and to prevent properties from being added or modified. This is the exact functionality provided by Object.freeze().
Let's break down what Object.freeze() does:
Makes existing properties non-writable: You cannot change their values.
Makes existing properties non-configurable: You cannot delete them or change their attributes (e.g., make them writable again).
Prevents new properties from being added: The object becomes sealed against any extensions.
After calling Object.freeze(obj), any attempt to add, modify, or delete properties will fail silently in non-strict mode or throw a TypeError in strict mode.
Example:
const myObject = {
prop1: 42,
prop2: 'Hello World'
};
Object.freeze(myObject);
myObject.prop1 = 100; // This assignment will fail silently (or throw an error in strict mode)
console.log(myObject.prop1); // Output: 42 (unchanged)
myObject.prop3 = 'New'; // Fails, cannot add new properties
delete myObject.prop2; // Fails, cannot delete properties
Now, let's evaluate why the other options are incorrect:
A. Object.const():
This method does not exist in JavaScript. The const keyword is used for declaring block-scoped variables, but it does not make the object itself immutable. A const object can still have its properties modified (e.g., const obj = {}; obj.a = 1; is allowed).
B. Object.eval():
This method does not exist for the purpose of immutability. The global eval() function is used to evaluate JavaScript code represented as a string, and its use is generally discouraged due to security and performance implications. It has nothing to do with controlling object properties.
C. Object.lock():
This method does not exist in standard JavaScript. It might be a distractor based on similar concepts in other programming languages.
Reference:
MDN Web Docs on Object.freeze():
Conclusion:
To fulfill the business requirement of making an object completely immutable—preventing any additions, modifications, or deletions of its properties—the correct method to use is Object.freeze().
Given the following code:
document.body.addEventListener(‘ click ’, (event) => {
if (/* CODE REPLACEMENT HERE */) {
console.log(‘button clicked!’);
)
});
Which replacement for the conditional statement on line 02 allows a developer to correctly determine that a button on page is clicked?
A. Event.clicked
B. e.nodeTarget ==this
C. event.target.nodeName == ‘BUTTON’
D. button.addEventListener(‘click’)
Explanation:
This code snippet uses event delegation, a powerful technique where a single event listener is placed on a parent element to manage events for all its child elements. The event listener is on the document.body, and it listens for a click event anywhere on the page.
The event.target property refers to the specific element that was clicked.
The nodeName property of an element returns the name of the node, which for HTML elements is the tag name in uppercase. For a button, this would be 'BUTTON'.
Therefore, by checking if event.target.nodeName is equal to 'BUTTON', the developer can accurately determine if the clicked element was a button.
Why Other Options Are Incorrect
A. Event.clicked:
clicked is not a standard property of the Event object. This would result in an error or an undefined value.
B. e.nodeTarget == this:
e.nodeTarget is not a standard property; the correct property is event.target.
In an arrow function, this refers to the parent scope, which in this case is the document.body. This comparison would only be true if the document.body itself was clicked, not a child button.
D. button.addEventListener('click'):
This is a method call, not a conditional statement. It would attempt to add an event listener inside the if condition, which is syntactically and logically incorrect.
Refer to the expression below:
Let x = (‘1’ + 2) == (6 * 2);
How should this expression be modified to ensure that evaluates to false?
A. Let x = (‘1’ + ‘ 2’) == ( 6 * 2);
B. Let x = (‘1’ + 2) == ( 6 * 2);
C. Let x = (1 + 2) == ( ‘6’ / 2);
D. Let x = (1 + 2 ) == ( 6 / 2);
Explanation:
🔍 Original Expression:
let x = ('1' + 2) == (6 * 2);
Breakdown:
'1' + 2 → '12' (string concatenation)
6 * 2 → 12 (number)
'12' == 12 → true (because == does type coercion)
❓Goal:
We want to modify the expression so it evaluates to false.
✅ Correct Answer:
A. let x = ('1' + ' 2') == (6 * 2);
Why?
'1' + ' 2' → '1 2' (note the space!)
6 * 2 → 12
'1 2' == 12 → false Because '1 2' is not coercible to the number 12.
❌ Why the other options fail:
B. '1' + 2 == 12
✅ true Same as original — still evaluates to true.
C. (1 + 2) == ('6' / 2)
✅ true 3 == 3 — both sides are numbers.
D. (1 + 2) == (6 / 2)
✅ true 3 == 3 again — still true.
🧠 Key Concepts:
+ with a string → triggers string concatenation
== → performs type coercion
To force false, you need non-equivalent values after coercion
A developer wants to set up a secure web server with Node.js. The developer creates a directory locally called app-server, and the first file is app-server/index.js Without using any third-party libraries, what should the developer add to index.js to create the secure web server?
A. const https =require(‘https’);
B. const server =require(‘secure-server’);
C. const tls = require(‘tls’);
D. const http =require(‘http’);
Explanation:
🔎 Key facts:
Node.js has a built-in https module for creating secure (SSL/TLS) web servers.
The http module is for plain, non-secure servers.
The tls module handles lower-level TLS/SSL sockets, but it’s not used directly to serve HTTPS web traffic.
There is no built-in secure-server module — that’s a fake distractor.
Check options:
A. const https = require('https'); ✅
Correct! This loads Node’s core https module, used to create secure HTTPS servers.
B. const server = require('secure-server'); ❌
Not a real Node.js core module.
C. const tls = require('tls'); ❌
This provides raw TLS socket support. Too low-level for setting up a web server.
D. const http = require('http'); ❌
This creates insecure HTTP servers, not HTTPS.
✅ Correct Answer: A. const https = require('https');
📚 Reference:
Node.js Docs – https module
Node.js Docs – http module
Page 2 out of 23 Pages |
Salesforce-JavaScript-Developer Practice Test Home |