Salesforce Certified JavaScript Developer JS-Dev-101 Exam Questions

Page: 1 / 14
Total 149 questions
Question 1

Which statement accurately describes an aspect of promises?



Answer : A

________________________________________

Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge

Evaluate each option:

________________________________________

A . Arguments for .then() are optional.

This is correct.

.then() has the signature:

promise.then(onFulfilled?, onRejected?)

Both arguments (onFulfilled and onRejected) are optional.

If a callback is not supplied, JavaScript provides a default pass-through handler.

________________________________________

B . .then() cannot be added after a catch.

Incorrect.

Promises support chaining in any order:

promise

.catch(...)

.then(...);

After a catch, the chain continues normally.

________________________________________

C . .then() manipulates and returns the original promise.

Incorrect.

.then() always returns a new promise, not the original one.

This is fundamental to promise chaining behavior.

________________________________________

D . Returning values in .then() is not necessary.

Incorrect.

If you want the next .then() in the chain to receive a value, the current .then() must explicitly return it:

.then(value => {

return value * 2; // passes to next .then()

})

If nothing is returned, the next .then() receives undefined.

________________________________________

Why A is correct

It is the only statement that accurately describes built-in Promise behavior:

.then() accepts optional arguments.

________________________________________

JavaScript Knowledge Reference (text-only)

.then(onFulfilled?, onRejected?) accepts optional handlers.

Promise chaining creates new promises for each .then().

catch() can be followed by additional .then() calls.

Returning inside .then() passes values to the next step in the chain.


Question 2

Which statement accurately describes the behavior of the async/await keywords?



Answer : D

________________________________________

Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge

When async is added to a function:

async function example() {}

JavaScript guarantees:

The function always returns a Promise, regardless of what is returned inside.

Inside the function, await pauses execution until a Promise resolves.

Code appears synchronous even though it uses asynchronous behavior.

Analysis of each option:

A incorrect:

Not 'sometimes'---an async function always returns a Promise.

B incorrect:

Async functions can be called just like normal functions.

C incorrect:

Async/await has nothing to do with classes specifically.

D correct:

This is the standard description:

''Async functions behave asynchronously but allow writing code that looks synchronous.''

________________________________________

JavaScript Knowledge Reference (text-only)

async functions always return Promises.

await pauses execution of the async function.

Async/await syntax creates synchronous-looking code on top of asynchronous operations.


Question 3

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.

Which two options can be inserted at line 02?



Answer : B, D

________________________________________

Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge

The correct constructor signature for CustomEvent is:

new CustomEvent(eventName, optionsObject)

Where:

eventName is a string.

optionsObject may include:

detail used to pass custom data

bubbles

cancelable, etc.

Example:

new CustomEvent('update', {

detail: { recordId: '123abc' }

});

Now evaluate each option:

________________________________________

Option A

{ type: 'update', recordId: '123abc' }

Incorrect: The constructor requires (eventName, options), not a single object. type is not used this way.

________________________________________

Option B

'update', { detail: { recordId: '123abc' } }

Correct format. detail is the proper place for custom event data.

________________________________________

Option C

'update', '123abc'

Incorrect: The second argument must be an object (options), not a string.

________________________________________

Option D

'update', { recordId: '123abc' }

Acceptable because any extra properties on the options object are still allowed, even though best practice is to use detail.

This still creates a valid CustomEvent, and the event will dispatch successfully.

Thus the two correct answers are B and D.

________________________________________

JavaScript Knowledge Reference (text-only)

new CustomEvent(name, options) is the required syntax.

The detail property of the options object is the standard location for custom data.

The second argument must be an object; other types are invalid.


Question 4

Why does second have access to variable a?



Answer : B

The variable a is declared in the outer function's scope. An inner function (second) forms a closure over its outer lexical scope, so it can access a. The reason is the scope chain / outer function's scope, not prototypes or hoisting.


Question 5

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

03 alert('Hey! I am John Doe');

04

05 }

06 button.addEventListener('click', listen);

Which two code lines make this code work as required?



Answer : C, D

Comprehensive and Detailed Explanation From Exact Extract JavaScript knowledge:

Requirement:

The message should be displayed only on the first click of the button.

Original behavior:

The handler listen is attached with button.addEventListener('click', listen);.

That means listen is called on every click until the listener is removed or configured otherwise.

Two correct ways to ensure the listener only fires once:

Use the once option in addEventListener

Remove the event listener after the first run inside the handler

Option C:

On line 06, add an option called once to button.addEventListener().

This means modifying line 06 to:

button.addEventListener('click', listen, { once: true });

The once: true option tells the browser:

Call the listen function at most once.

After it is called the first time, automatically remove the listener.

So:

First click: alert shows, listener is removed automatically.

Subsequent clicks: no further calls to listen, no alerts.

This satisfies the requirement.

Option D:

On line 04, use button.removeEventListener('click', listen);

This means updating the handler:

function listen(event) {

alert('Hey! I am John Doe');

button.removeEventListener('click', listen);

}

Now:

On the first click, listen is executed:

It shows the alert.

It removes itself from the button's click listeners.

On subsequent clicks, listen is no longer registered, so nothing happens.

This also satisfies the requirement.

Why A and B are incorrect:

Option A:

On line 04, use event.stopPropagation();

event.stopPropagation() stops the event from bubbling up the DOM tree.

It does not prevent the current listener from being called again in the future.

The click handler will still run on every click; it just prevents other listeners higher in the DOM from receiving the event.

Option B:

On line 02, use event.first to test if it is the first execution.

There is no built-in event.first property in standard DOM events.

This property does not exist and will be undefined.

You would need your own external flag (let hasRun = false;) to track first execution, but that is not what B describes.

Therefore, the two correct modifications are:

Study Guide / Concept Reference (no links):

addEventListener options object: { once: true }

removeEventListener to manually deregister event handlers

Event propagation (event.stopPropagation) vs handler lifecycle

DOM event model and listener registration

________________________________________


Question 6

Refer to the code below:

01

02

03

04

05

06

Click me!

07

16

Which code change should be done for the console to log the following when "Click me!" is clicked?

Row log

Table log



Answer : D

Current behavior:

Clicking <td> triggers the click event on row1, then bubbles up to <table>.

printMessage runs, logs 'Row log', then event.stopPropagation() stops the event from bubbling to the table.

So 'Table log' never appears.

To allow the table's inline onclick to run after the row handler:

Remove the propagation stop:

function printMessage(event) {

console.log('Row log');

// event.stopPropagation(); // remove this

}

Now the event bubbles:

printMessage logs 'Row log'.

The table's onclick runs, logging 'Table log'.

Option A only changes capture/bubble phase but still stops propagation. C does nothing meaningful (stopPropagation takes no arguments). B removes the row handler entirely.

________________________________________


Question 7

Refer to the code below:

const searchText = 'Yay! Salesforce is amazing!';

let result1 = searchText.search(/sales/i);

let result2 = searchText.search(/sales/);

console.log(result1);

console.log(result2);

After running this code, which result is displayed on the console?



Answer : D

Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge:

String: 'Yay! Salesforce is amazing!'

Index positions:

'Y' at 0, 'a' at 1, 'y' at 2, '!' at 3, space at 4, 'S' at 5, 'a' at 6, 'l' at 7, 'e' at 8, 's' at 9, etc.

Substring 'Sales' starts at index 5.

String.prototype.search with a regex returns the index of the first match or -1 if there is no match.

searchText.search(/sales/i);

/sales/i is case-insensitive because of the i flag.

It matches 'Sales' beginning at index 5.

So result1 is 5.

searchText.search(/sales/);

/sales/ is case-sensitive.

It requires lowercase 'sales'.

The text has 'Sales' with uppercase S, so this does not match.

search returns -1 when there is no match.

So result2 is -1.

Console output:

First log: 5

Second log: -1

Option D matches this.

Concepts: regex search, case sensitivity vs i flag, String.prototype.search return values.

________________________________________


Page:    1 / 14   
Total 149 questions