# Free SJDI practice questions

> 10 free SJDI practice questions with the correct answer and an explanation.

Page: https://devopscodelab.com/exams/salesforce-javascript-developer-i/free-practice-questions/
Practice exams: https://devopscodelab.com/exams/salesforce-javascript-developer-i/

### Question 1 of 10

A developer attaches metadata to DOM elements that are frequently removed from the page. The metadata must not keep removed elements alive in memory, and the code never needs to iterate over all entries. Which approach meets these requirements?

- A. Store the metadata in a WeakMap keyed by the element object
- B. Store the metadata in a Map keyed by the element object
- C. Store the metadata in a Set of { element, data } wrapper objects
- D. Store the metadata on a global object keyed by each element's id

**Answer:** A

**Explanation:** WeakMap holds its keys weakly, so once an element is removed and no other references exist, both the key and its value can be garbage collected; WeakMaps are not iterable, which matches the requirement. A Map or a Set holds strong references, keeping removed elements in memory until entries are deleted manually. A global object keyed by id strings also retains the data indefinitely and breaks for elements without an id.

### Question 2 of 10

A Lightning web component stores a list of selected record Ids and must reject duplicates automatically while preserving insertion order. Lookups such as "is this Id already selected?" must be fast. Which data structure should the developer use?

- A. A WeakSet, adding Ids with add() so unused entries are garbage collected
- B. A Set, adding Ids with add() and checking membership with has()
- C. A plain object keyed by Id, iterating with for...in to read the selections
- D. An Array, checking each new Id with indexOf() before calling push()

**Answer:** B

**Explanation:** A Set stores unique values, keeps insertion order when iterated, and offers has() for constant-time membership checks. An Array with indexOf() works but checks are linear and duplicates are only prevented manually. A WeakSet accepts only objects, not string Ids, and cannot be iterated. A plain object does not guarantee insertion order for integer-like keys and is less direct than a Set for this purpose.

### Question 3 of 10

A developer invokes a function with an explicit this value:
function greet(greeting, punctuation) {
  return ${greeting}, ${this.name}${punctuation};
}
const person = { name: 'Rae' };
console.log(greet.call(person, 'Hi', '!'), greet.apply(person, ['Hey', '?']));
What is logged?

- A. Hi, undefined! Hey, undefined?
- B. Hi, Rae! Hey,?, Raeundefined
- C. Hi, Rae! Hey, Rae?
- D. Hi, Rae! followed by a thrown TypeError

**Answer:** C

**Explanation:** call() takes this followed by individual arguments, and apply() takes this followed by an array of arguments that is spread into the parameters; both set this to person, so name resolves to 'Rae'. undefined for the name would appear only if the function were called without a receiver. Treating the array as a single greeting argument describes call(), not apply(), and apply accepts an array, so no TypeError is thrown.

### Question 4 of 10

A developer is comparing method styles in a Node.js CommonJS script:
const counter = {
  count: 10,
  regular() { return this.count; },
  arrow: () => typeof this
};
console.log(counter.regular(), counter.arrow());
What is logged?

- A. 10 object
- B. 10 number
- C. 10 undefined
- D. undefined object

**Answer:** A

**Explanation:** A method defined with shorthand syntax receives the calling object as this, so regular() returns 10. Arrow functions do not bind their own this; they capture it from the enclosing scope, which at the top of a CommonJS module is module.exports, an object. The arrow function therefore cannot see counter, and typeof this is 'object' rather than 'number'; it is not undefined because module scope this is an object.

### Question 5 of 10

A script appends 1,000 rows to a table one at a time with table.appendChild(row) inside a loop, and between insertions it reads table.offsetHeight. The page becomes janky. Which change improves performance the MOST?

- A. Wrap each appendChild call in setTimeout so that every row is inserted in a separate task
- B. Build the rows in a DocumentFragment, append it once, and read offsetHeight only afterwards
- C. Read offsetHeight before every insertion instead of after it so that layout stays cached
- D. Replace appendChild with innerHTML += for each row so that the browser parses all of them faster

**Answer:** B

**Explanation:** Interleaving DOM writes with layout reads forces a synchronous reflow on every iteration (layout thrashing). Building rows in a DocumentFragment and inserting them in one operation, then reading layout once, avoids repeated reflows. innerHTML += re-parses the whole table each time, separate timers spread the same expensive work across many tasks, and reading before each write still forces a layout after every modification.

### Question 6 of 10

A list contains hundreds of <li> elements that are added and removed dynamically. The developer needs to handle clicks on any item with minimal memory use and without re-registering listeners when items change. Which approach should be used?

- A. Set the onclick property of every <li> inside a MutationObserver callback
- B. Add a click listener to each <li> when it is created and remove it when it is deleted
- C. Add a click listener to each <li> with { capture: true } so new items inherit it
- D. Add one click listener to the parent <ul> and use event.target.closest('li') to find the item

**Answer:** D

**Explanation:** Event delegation relies on click events bubbling from each item to the parent, so a single listener on the <ul> handles current and future items, and closest('li') identifies which item was clicked. Per-item listeners, whether added directly or through a MutationObserver, need constant bookkeeping and use more memory. The capture option only changes the phase in which a listener runs; it does not make listeners apply to newly created elements.

### Question 7 of 10 (choose 2)

A developer must step through a Lightning web component's client-side JavaScript in a Salesforce org with readable framework code. Which two actions help? (Choose two.)

- A. Enable debug mode for the user in Setup so that framework code is not minified
- B. Use the Sources panel in browser DevTools to set breakpoints in the component's JavaScript
- C. Deploy the component with the API version set to debug
- D. Add System.debug() statements to the component's JavaScript file
- E. Open the Developer Console log for the component's client-side code

**Answer:** A, B

**Explanation:** Debug mode makes Lightning serve unminified framework code with more readable output, and browser DevTools breakpoints let the developer step through the component's JavaScript. System.debug is an Apex method and does not exist in JavaScript, and Developer Console debug logs capture server-side Apex execution rather than browser code. API version numbers do not provide a debug setting.

### Question 8 of 10 (choose 2)

A dashboard must check a job status every five seconds until it completes, without blocking the UI. Which two approaches are valid? (Choose two.)

- A. requestIdleCallback(poll, 5000) to repeat the poll every five seconds
- B. A recursive setTimeout that schedules the next poll after each one finishes
- C. const id = setInterval(poll, 5000); clearInterval(id) when done
- D. Promise.all([poll(), 5000]) to repeat the poll after five seconds
- E. while (true) { poll(); } with a 5-second sleep inside the loop

**Answer:** B, C

**Explanation:** setInterval repeats the callback every five seconds until clearInterval is called, and a recursive setTimeout schedules each poll after the previous one, which avoids overlapping requests. JavaScript has no blocking sleep, and an infinite loop would freeze the page. requestIdleCallback runs once when the browser is idle, its option is a timeout rather than a repeat interval, and Promise.all does not create any delay or repetition.

### Question 9 of 10

A team clones a Node.js project in a CI pipeline and needs a clean install that uses exactly the versions recorded in package-lock.json, failing if the lock file and package.json disagree. Which command should the pipeline run?

- A. npm audit fix
- B. npm update
- C. npm ci
- D. npm install

**Answer:** C

**Explanation:** npm ci removes any existing node_modules, installs exactly what package-lock.json specifies, and errors out if the lock file is out of sync with package.json, which makes builds reproducible. npm install may update the lock file, npm update deliberately moves to newer allowed versions, and npm audit fix changes versions to patch vulnerabilities.

### Question 10 of 10

A Jest test for a Lightning web component changes a public property on the element and then immediately queries the shadow DOM for the updated text, but the assertion fails. What should the developer do?

- A. Set the property with setAttribute() instead of assigning it in JavaScript
- B. Wait for a resolved promise so the re-render finishes, then assert
- C. Query document.body.innerHTML instead of the component's shadowRoot property
- D. Call element.render() on the component before running the assertion again

**Answer:** B

**Explanation:** LWC re-renders asynchronously after a property change, so the test must wait a microtask, typically by returning a resolved promise and asserting in then(). Components do not expose a render() method to call directly. The text lives inside the component's shadow root, so querying document.body.innerHTML does not reach it, and setAttribute does not avoid the asynchronous re-render.

Ready for a timed run? Take the free 15-question SJDI mini exam: https://devopscodelab.com/app/exams/salesforce-javascript-developer-i/mini/

DevOpsCode Lab is not affiliated with Salesforce. SJDI and related names are trademarks of their owners. We write every question ourselves; these are not real exam questions.
