Salesforce Certified JavaScript Developer JS-Dev-101 Exam Questions

Page: 1 / 14
Total 147 questions
Question 1

Refer to the code below:

01 let country = {

02 get capital() {

03 let city = Number("London");

04

05 return {

06 cityString: city.toString(),

07 }

08 }

09 }

Which value can a developer expect when referencing country.capital.cityString?



Answer : B

In the getter:

let city = Number('London');

The Number() constructor attempts to convert the string 'London' into a numeric value.

'London' is not a valid numeric string.

When JavaScript attempts numeric conversion of a non-numeric string:

Number('London') NaN

Next line:

city.toString()

When city is NaN, calling .toString() yields:

NaN.toString() 'NaN'

The getter returns:

{

cityString: 'NaN'

}

The question asks:

Which value can a developer expect?

The multiple-choice options include NaN, but not 'NaN'.

Given the available choices, the intended correct answer is B (NaN) because the root cause is the Number('London') conversion returning NaN.

JavaScript Knowledge Reference (text-only)

Number(nonNumericString) returns NaN.

NaN.toString() produces 'NaN'.

Getters compute and return their values each time the property is accessed.

==================================================


Question 2

A team at Universal Containers works on a big project and uses yarn to manage the project's dependencies.

A developer added a dependency to manipulate dates and pushed the updates to the remote repository. The rest of the team complains that the dependency does not get downloaded when they execute yarn.

What could be the reason for this?



Answer : C

In JavaScript server-side development using Node.js, dependency management is typically handled through package managers such as npm or yarn. These tools categorize installed packages into:

dependencies -- required for running the application in any environment

devDependencies -- required only during development (testing tools, build tools, documentation generators, etc.)

When a package is installed using:

yarn add --dev

it is placed under the 'devDependencies' section of package.json.

Behavior of Production Mode

Node.js uses the environment variable:

NODE_ENV=production

When this environment variable is set to production, both npm and Yarn follow the standard Node.js convention and skip installing devDependencies. This is done to optimize production builds and reduce deployment size. This is a known and documented behavior in Node.js package management tools.

Therefore, if:

The developer added the date-manipulation library as a dev dependency, and

Other team members execute yarn in an environment where NODE_ENV=production is set,

then Yarn will not install that dependency because devDependencies are intentionally excluded in production mode.

This explains the behavior described in the question.

Why the Other Options Are Incorrect

Option A:

'YARN_ENV is set to production' is incorrect because Yarn does not use the variable YARN_ENV for dependency installation behavior. Node.js tools use NODE_ENV, not YARN_ENV.

Option B:

This is incorrect because Yarn automatically writes dependencies into package.json. Unlike older npm versions, there is no need for the --save flag.

Option D:

There is no such option as --add. The correct syntax is simply:

yarn add

Missing an option that does not exist cannot be the cause.

JavaScript Knowledge Reference

Node.js uses the environment variable NODE_ENV to determine production or development mode.

Package managers (npm and Yarn) follow the rule that when NODE_ENV=production, only 'dependencies' are installed and 'devDependencies' are skipped.

Yarn automatically persists installed packages to package.json without requiring --save.

Yarn uses the command yarn add to add dependencies; there is no --add flag.


Question 3

A developer has an isDeg function that takes one argument, pts. They want to schedule the function to run every minute.

What is the correct syntax for scheduling this function?



Answer : B

setInterval(fn, delay, arg1, arg2, ...) calls fn every delay ms, passing extra arguments.

To call isDeg every 60 seconds with 'cat':

setInterval(isDeg, 60000, 'cat');

Why others are wrong:

A/D: isDeg('cat') is called immediately, and its return value is scheduled, not the function.

C: setTimeout runs once after 60 seconds, not repeatedly.


Question 4

A developer wrote the following code:

01 let x = object.value;

02

03 try {

04 handleObjectValue(x);

05 } catch(error) {

06 handleError(error);

07 }

The developer has a getNextValue function to execute after handleObjectValue(), but does not want to execute getNextValue() if an error occurs. How can the developer change the code to ensure this behavior?



Answer : B

Requirement:

getNextValue() should run only if handleObjectValue(x) does not throw an error.

If an error occurs, handleError(error) should run, and getNextValue() should be skipped.

Option B:

try {

handleObjectValue(x);

getNextValue();

} catch (error) {

handleError(error);

}

Behavior:

If handleObjectValue(x) succeeds (no error):

Execution continues to getNextValue();.

If handleObjectValue(x) throws:

Control jumps directly to catch, getNextValue() is skipped.

handleError(error); is called.

This matches the requirement perfectly.

Why others are incorrect:

A: } then { is invalid JavaScript syntax.

C:

try {

handleObjectValue(x);

} catch (error) {

handleError(error);

}

getNextValue();

getNextValue() is called after the try...catch regardless of whether an error occurred. Not acceptable.

D: finally always runs:

try {

handleObjectValue(x);

} catch (error) {

handleError(error);

} finally {

getNextValue();

}

getNextValue() will execute in both success and error cases.

Therefore, the correct approach is B.

Concepts: try/catch control flow, finally semantics, placing code inside vs outside try/catch blocks.


Question 5

A developer implements a function that adds a few values.

function sum(num1, num2, num3) {

if (num3 === undefined) {

num3 = 0;

}

return num1 + num2 + num3;

}

Which three options can the developer invoke for this function to get a return value of 10?



Answer : C, D, E

The verified corrected answers are C, D, and E.

This function is a normal function, not a curried function:

function sum(num1, num2, num3) {

if (num3 === undefined) {

num3 = 0;

}

return num1 + num2 + num3;

}

It expects values to be passed in the same function call:

sum(num1, num2, num3);

Now check the valid corrected options.

Option C:

sum(5, 5, 0);

Calculation:

5 + 5 + 0

Result:

10

So C is correct.

Option D:

sum(10, 0);

Here, num3 is not provided, so it is undefined.

The function checks:

if (num3 === undefined) {

num3 = 0;

}

So the calculation becomes:

10 + 0 + 0

Result:

10

So D is correct.

Option E:

sum(5, 2, 3);

Calculation:

5 + 2 + 3

Result:

10

So E is correct.

Why A and B are incorrect as originally written:

sum(5)(5);

This calls sum(5) first. Since num2 is missing, the result becomes NaN. Then JavaScript tries to call that returned value as a function, which causes a TypeError.

sum()(10);

This also calls sum() first, producing NaN, and then attempts to call NaN as a function.

Those styles would only work if sum were written as a curried function, but the given implementation is not curried.

Therefore, the verified corrected answers are C, D, and E.


Question 6

A developer has two ways to write a function:

Option A:

01 function Monster() {

02 this.growl = () => {

03 console.log("Grr!");

04 }

05 }

Option B:

01 function Monster() {};

02 Monster.prototype.growl = () => {

03 console.log("Grr!");

04 }

After deciding on an option, the developer creates 1000 monster objects. How many growl methods are created with Option A and Option B?



Answer : C

Option A:

this.growl = () => { ... } inside the constructor creates a new function per instance.

For 1000 new Monster(), you get 1000 distinct growl functions, each stored on the instance itself.

Option B:

Monster.prototype.growl = () => { ... } attaches growl to the prototype.

All instances share the same single growl function via the prototype chain.

For 1000 instances, only 1 growl method exists in memory.

So: Option A 1000; Option B 1.


Question 7

A developer initiates a server with the file server.js and adds dependencies in the source code's package.json that are required to run the server.

Which command should the developer run to start the server locally?



Answer : B

Comprehensive and Detailed Explanation From JavaScript/Node.js Knowledge:

In a Node.js project that uses package.json, you typically define a 'start' script:

{

'scripts': {

'start': 'node server.js'

}

}

Then you start the app with:

npm start

npm start:

Looks up the 'start' script in package.json.

Runs the command defined there (commonly node server.js).

This is the standard way to start a Node.js app with npm-managed dependencies.

Why others are incorrect:

A . node start

Tries to run a file named start with Node; does not use package.json scripts.

C . npm start server.js

npm start does not take the script filename as an argument in this way; it just runs the start script as defined.

D . start server.js

Not an npm or node command; on some shells it just tries to ''start'' a process but is not the standard Node/npm workflow.

Relevant concepts: package.json, npm scripts, npm start, Node entry file execution.


Page:    1 / 14   
Total 147 questions