Free Salesforce JavaScript Developer Practice Test Questions
Topic: Browser and Events
147 questionsUpdated 23-Nov-20267 topics
TopicBrowser and Events
Question 1Choose 2
After user acceptance testing, the developer is asked to change the webpage background based on the user’s location. It works on the developer’s computer but not on the tester’s machine.
Which two actions will help determine accurate results?
Correct answer:
A. The tester should disable their browser cache.
C. The tester should clear their browser cache.
Explanation:
This question tests your understanding of browser caching issues and debugging techniques when code works in one environment but not another. When a webpage update works on the developer's machine but not on the tester's machine, the most common culprit is the browser cache serving stale files. Caching issues are a frequent source of inconsistent behavior across environments and can be resolved by disabling or clearing the cache. Reworking the code or inspecting refresh settings are not the primary solutions to this problem.
Correct Options:
A. The tester should disable their browser cache –
Correct. Disabling the browser cache ensures that the tester's browser always fetches the latest version of all resources (HTML, CSS, JavaScript) from the server rather than serving cached files. This is a reliable way to confirm that the observed behavior is based on the current code and not on older cached assets. Developers often use this approach during testing to eliminate caching variables.
C. The tester should clear their browser cache –
Correct. Clearing the browser cache removes all stored files, forcing the browser to download fresh copies from the server on the next page load. If the issue is caused by an outdated cached stylesheet or script, clearing the cache will resolve it. This is a standard first step in troubleshooting environment-specific issues where code changes are not reflecting.
Incorrect Options:
B. The developer should inspect their browser refresh settings –
Incorrect. The developer's machine is already displaying the correct behavior, so inspecting refresh settings on their machine does not help diagnose why the tester's machine behaves differently. The problem is localized to the tester's environment, so the focus should be on the tester's browser configuration, not the developer's.
D. The developer should rework the code –
Incorrect. The code works correctly on the developer's computer, which indicates that the code itself is likely functional. Reworking the code would be premature and unnecessary when the issue is almost certainly related to caching or environment differences. The correct approach is to first eliminate caching as a variable before considering code changes.
Reference:
MDN Web Docs – HTTP caching and browser cache behavior
Google Chrome DevTools – Disable cache and network throttling
Salesforce Trailhead – Debugging and Testing Lightning Web Components
Salesforce Developer Guide – Browser caching best practices for development
Question 2Choose 1
Given the code below:
01 setCurrentUrl();
02 console.log( " The current URL is: " + url);
03
04 function setCurrentUrl() {
05 url = window.location.href;
06 }
What happens when the code executes?
Correct answer:
B. The url variable has global scope and line 02 executes correctly.
Explanation:
This question tests your understanding of variable scope and assignment in JavaScript, specifically the behavior of variables declared without var, let, or const. When a variable is assigned a value without any declaration keyword, it becomes an implicit global property on the window object. In non-strict mode, this creates a global variable that is accessible anywhere in the code, allowing line 02 to access and log the value successfully.
Correct Option:
B. The url variable has global scope and line 02 executes correctly –
Correct. Inside the setCurrentUrl() function, the variable url is assigned without using var, let, or const. In non-strict mode (the default in most JavaScript environments), this creates an implicit global variable attached to the window object. Therefore, url is globally accessible, and line 02 can read and log its value without throwing an error. The code executes successfully, and the URL is displayed in the console.
Incorrect Options:
A. The url variable has global scope and line 02 throws an error –
Incorrect. While it is correct that url has global scope, line 02 does not throw an error. Because the variable is global, it is accessible from anywhere in the code. The function setCurrentUrl() is called on line 01 before line 02 executes, so url is already assigned a value when line 02 runs, preventing any ReferenceError.
C. The url variable has local scope and line 02 executes correctly –
Incorrect. The url variable does not have local scope because it is not declared with var, let, or const inside the function. Without a declaration keyword, the variable does not become scoped to the function; instead, it becomes a global property. Local scope would only apply if var url, let url, or const url were used inside the function.
D. The url variable has local scope and line 02 throws an error –
Incorrect. This combines two incorrect claims. First, url is not locally scoped because no declaration keyword is used. Second, line 02 does not throw an error because the variable is globally accessible and has been assigned a value by the time line 02 executes.
Reference:
MDN Web Docs – Implicit globals and variable scope
MDN Web Docs – Global object (window) in browsers
MDN Web Docs – Strict mode and implicit global assignment
Salesforce Trailhead – JavaScript Essentials: Variables and Scope
Question 3Choose 2
Refer to the code:
01 const event = new CustomEvent(
02 // Missing code
03 );
04 obj.dispatchEvent(event);
A developer needs to dispatch a custom event called update to send information about recordId.
Explanation:
This question tests your knowledge of the CustomEvent constructor in JavaScript. The CustomEvent constructor takes two parameters: the first is a string representing the event type (or name), and the second is an optional options object. To send additional data with the event, you must use the detail property within the options object. Any custom data must be placed inside the detail property for it to be accessible via event.detail when the event is handled.
Correct Options:
B. ' update ' , { detail: { recordId: ' 123abc ' } } –
Correct. The first argument is the event type 'update', and the second argument is an options object containing the detail property with the custom data. This is the proper syntax for sending additional information with a CustomEvent. The data can later be accessed using event.detail.recordId in the event listener, making this a valid implementation.
D. ' update ' , { recordId: ' 123abc ' } –
Correct. The first argument is the event type 'update', and the second argument is an options object. While the detail property is the official recommended way to pass custom data, the CustomEvent constructor also allows any properties to be added directly to the options object in some environments. However, to properly access the data in the event listener, you would need to use event.recordId or other approaches. In many implementations, this will work because the options object properties are merged into the event. It is functionally acceptable for dispatching the event.
Incorrect Options:
A. { type: ' update ' , recordId: ' 123abc ' } –
Incorrect. The CustomEvent constructor expects the event type as the first argument (a string), not as a property inside an object. Passing a single object as the first argument would be interpreted as the event type, and the object would be coerced to a string like "[object Object]", which is not the desired event name. Additionally, the recordId would not be accessible because it is not placed inside the detail property or as a second argument.
C. ' update ' , ' 123abc ' –
Incorrect. While the first argument 'update' correctly specifies the event type, the second argument must be an options object, not a primitive string. The CustomEvent constructor expects the second parameter to be an object (optionally containing detail, bubbles, cancelable, etc.). Passing a string as the second argument would result in the data being ignored or cause a type error, and the recordId would not be accessible in the event listener.
Reference:
MDN Web Docs – CustomEvent constructor and usage
MDN Web Docs – CustomEvent.detail property
MDN Web Docs – EventTarget.dispatchEvent() method
Salesforce Trailhead – Lightning Web Components: Communicating with Events
Question 4Choose 1
A developer needs the function personalizeWebsiteContent to run when the webpage is fully loaded (HTML and all external resources).
Which implementation should be used?
Correct answer:
B. Add a listener to the window object to handle the load event
Explanation:
This question tests your understanding of browser lifecycle events, specifically the difference between DOMContentLoaded and load events. The load event fires when the entire webpage has fully loaded, including all external resources such as stylesheets, images, scripts, and subframes. The DOMContentLoaded event fires when the HTML document has been completely parsed and the DOM tree is built, but it does not wait for external resources like images and stylesheets. Since the requirement is for the function to run when the webpage is fully loaded with all external resources, the load event on the window object is the correct choice.
Correct Option:
B. Add a listener to the window object to handle the load event –
Correct. The window object's load event fires after the entire page and all dependent resources (CSS, images, frames, etc.) have finished loading. Adding a listener to this event ensures that personalizeWebsiteContent runs only when everything is fully loaded. This is the standard and most reliable way to execute code after all page resources are available.
Incorrect Options:
A. Add a handler to the personalizeWebsiteContent script to handle the DOMContentLoaded event –
Incorrect. The DOMContentLoaded event fires when the HTML document has been parsed and the DOM is ready, but it does not wait for external resources like images, stylesheets, or fonts to load. If personalizeWebsiteContent relies on those resources, it may execute before they are available, leading to potential errors or unexpected behavior.
C. Add a listener to the window object to handle the DOMContentLoaded event –
Incorrect. While this syntax is valid, the DOMContentLoaded event fires too early for the requirement. It fires after the DOM is parsed but before images and other external resources are fully loaded. Since the requirement explicitly states "HTML and all external resources," the load event is the appropriate choice, not DOMContentLoaded.
D. Add a handler to the personalizeWebsiteContent script to handle the load event –
Incorrect. The wording "add a handler to the personalizeWebsiteContent script" is ambiguous and not a standard event-listening approach. Event listeners should be attached to DOM elements or the window object, not directly to scripts. Additionally, even if interpreted as attaching to the script element, the load event on a script element fires when that specific script loads, not when the entire page and all resources are fully loaded.
Reference:
MDN Web Docs – Window: load event
MDN Web Docs – DOMContentLoaded event vs load event
MDN Web Docs – Document: DOMContentLoaded event
Salesforce Trailhead – Working with Browser Events in JavaScript
MDN Web Docs – EventTarget.addEventListener() method
Question 5Choose 1
HTML:
< p > The current status of an Order: < span id= " status " > In Progress < /span > < /p >
Which JavaScript statement changes ' In Progress ' to ' Completed ' ?
Correct answer:
C. document.getElementById( " status " ).innerHTML = ' Completed ' ;
Explanation:
This question tests your knowledge of DOM manipulation in JavaScript, specifically using getElementById() to select an element and modify its content. The getElementById() method expects the element's ID as a string without any CSS selector prefixes like # or .. Once the element is selected, the innerHTML property is used to change its HTML content. The value property is used for form input elements, not for generic HTML elements like . Therefore, the correct syntax is document.getElementById("status").innerHTML = 'Completed';.
Correct Option:
C. document.getElementById("status").innerHTML = 'Completed'; –
Correct. The getElementById() method correctly takes the ID string "status" without any prefixes. The innerHTML property is then used to replace the content of the element from 'In Progress' to 'Completed'. This is the standard and correct way to update the text content of a DOM element using its ID.
Incorrect Options:
A. document.getElementById(".status").innerHTML = 'Completed'; –
Incorrect. The getElementById() method does not accept CSS selector syntax like "." for classes. The string ".status" would be interpreted as an ID literally containing a dot, not as a class selector. Since no element has the ID ".status", this would return null and cause a runtime error when trying to access innerHTML.
B. document.getElementById("#status").innerHTML = 'Completed'; –
Incorrect. Similar to option A, getElementById() does not accept CSS selector syntax like "#" for IDs. The string "#status" would be interpreted as an ID literally containing a hash symbol. Since no element has the ID "#status", this would return null and throw an error. The # prefix is used with querySelector(), not getElementById().
D. document.getElementById("status").Value = 'Completed'; –
Incorrect. While the element selection part is correct, the property used is wrong. The value property is used for form elements like ,
That's the last free question in Browser and Events
You've seen the free 25% of this topic (5 of 20 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.