Salesforce certification · Exam Salesforce-JavaScript-Developer

Salesforce-JavaScript-Developer Mock Exam

Salesforce JavaScript Developer - JS-Dev-101

Free Salesforce-JavaScript-Developer questions on Objects, Functions, and Classes, each with the answer and an explanation.

20 questions · 33 minutes · free, no sign-up

Objects, Functions, and Classes questions

1–10 of 10 free
Question 1 Choose 2
Refer to the code below:

01 const addBy = ?

02 const addByEight = addBy(8);

03 const sum = addByEight(50);

Which two functions can replace line 01 and return 58 to sum?
Show answerHide answer

Correct answer: A, D const addBy = function(num1) {

return function(num2) {

return num1 + num2;

}

}
(Corrected for typing errors)

const addBy = (num1) = > {

return function(num2) {

return num1 + num2;

}

}

Explanation

Explanation:
This question tests understanding of closures and higher-order functions in JavaScript. The code pattern shows addBy being called with 8 (returning a function), then that returned function is called with 50 to produce 58. This requires addBy to be a function that returns another function which captures the first argument (num1) and adds it to the second argument (num2) when invoked.

Correct Options:

A – Correct.
This defines addBy as a function that takes num1 and returns a new function that takes num2 and returns num1 + num2. When addBy(8) is called, it returns function(num2) { return 8 + num2; }. Then addByEight(50) adds 8 + 50 = 58. This is a proper closure implementation.

D – Correct.
This is the arrow function equivalent of option A. addBy is defined as an arrow function that takes num1 and returns an inner function (using traditional function syntax) that takes num2 and returns num1 + num2. It works identically to option A, creating a closure that captures num1 and returns 58 when called with 50.

Incorrect Options:

B – Incorrect.
This function takes only one parameter num1 but tries to use num2 which is not defined in its scope. This would throw a ReferenceError because num2 is not accessible. Additionally, it doesn't return a function as required by the pattern.

C – Incorrect.
This arrow function (num1) => num1 + num2 also tries to reference num2 which is not defined in its scope. It would throw a ReferenceError. Moreover, it doesn't return a function, so addBy(8) would return a number (or error), not a function that can be called with 50.

Reference:
MDN Web Docs - Closures and Function returning functions; Salesforce LWC JavaScript documentation on higher-order functions and closures.
Question 2 Choose 1
A developer imports:

import printPrice from ' /path/PricePrettyPrint.js ' ;

What must be true about printPrice for this import to work?
Show answerHide answer

Correct answer: C printPrice must be the default export

Explanation

Explanation:
This question tests your knowledge of ES6 module syntax, specifically the difference between default and named imports. In JavaScript, when you import a module without curly braces ({}), you are explicitly importing the default export from that module. The naming of the imported variable is flexible because it refers to the default binding.

Correct Option:

C. printPrice must be the default export
This is correct because import printPrice from '...' uses the default import syntax. In the exported file (PricePrettyPrint.js), the code must contain export default followed by a function, class, or object. Default imports do not require curly braces and can be assigned any local name. The file likely exports export default function printPrice() {...} or similar.

Incorrect Option:

A. printPrice must be a named export –
Incorrect. Named exports require curly braces during import, e.g., import { printPrice } from '...'. Without the braces, the JavaScript engine looks for a default export only. A named export would cause a runtime error because no default binding exists.

B. printPrice must be an all export –
Incorrect. "All export" is not a standard ES6 term. This likely confuses with export * or namespace imports (import * as), which are not applicable here. Namespace imports also use a different syntax and do not map to a single default variable.

D. printPrice must be a multi export –
Incorrect. "Multi export" is not a valid ES6 concept. The closest interpretation might be multiple named exports, but again, those require destructuring with {}. Default exports are singular and cannot be mixed with this import style unless explicitly declared as default.

Reference:

MDN Web Docs – import statement (ES Module syntax)

Salesforce Trailhead – Lightning Web Components Basics: Use JavaScript in LWC

Salesforce Developer Guide – JavaScript Modules in Lightning Web Components
Question 3 Choose 2
JavaScript:

01 function Tiger() {

02 this.type = ' Cat ' ;

03 this.size = ' large ' ;

04 }

05

06 let tony = new Tiger();

07 tony.roar = () = > {

08 console.log( ' They\ ' re great! ' );

09 };

10

11 function Lion() {

12 this.type = ' Cat ' ;

13 this.size = ' large ' ;

14 }

15

16 let leo = new Lion();

17 // Insert code here

18 leo.roar();

Which two statements could be inserted at line 17 to enable line 18?
Show answerHide answer

Correct answer: A, B leo.roar = () = > { console.log( ' They\ ' re pretty good! ' ); };Object.assign(leo, tony);

Explanation

Explanation:
This question tests your understanding of object property assignment, prototypal inheritance, and the Object.assign() method in JavaScript. Line 18 calls leo.roar(), so the roar method must exist on leo itself or somewhere in its prototype chain before that line executes. The inserted code must ensure leo.roar is defined at the time of invocation.

Correct Options:

A. leo.roar = () => { console.log('They\'re pretty good!'); }; –
Correct. This directly adds a roar property to the leo object instance itself. Since leo is an instance of Lion, adding the method as an own property makes it immediately available. When line 18 executes, leo.roar() finds the method directly on the object and invokes it successfully.

B. Object.assign(leo, tony); –
Correct. The Object.assign() method copies all enumerable own properties from the source object (tony) to the target object (leo). Since tony has a roar method (added on line 07), this method gets copied to leo as an own property. When line 18 executes, leo.roar() exists and works correctly.

Incorrect Options:

C. Object.assign(leo, Tiger); –
Incorrect. Tiger is a constructor function, not an object instance. Object.assign() copies properties from the source object's own enumerable properties. The Tiger function has no roar property defined on it; the roar method exists only on the tony instance. Copying from Tiger would not add the roar method to leo.

D. leo.prototype.roar = () => { console.log('They\'re pretty good!'); }; –
Incorrect. leo is an object instance, not a constructor function. Instances do not have a prototype property; they have an internal [[Prototype]] (accessible via __proto__ or Object.getPrototypeOf()). Assigning to leo.prototype creates an own property named prototype on leo itself, which does not affect the method lookup for leo.roar(). The prototype property only exists on constructor functions, not on their instances.

Reference:

MDN Web Docs – Object.assign() method

MDN Web Docs – Working with objects and property assignment

MDN Web Docs – Inheritance and the prototype chain

Salesforce Trailhead – JavaScript Essentials: Objects and Prototypes
Question 4 Choose 1
Refer to the code below:

flag();

function flag() {

console.log( ' flag ' );

}

const anotherFlag = () = > {

console.log( ' another flag ' );

}

anotherFlag();

What is result of the code block?
Show answerHide answer

Correct answer: D The console logs ' flag ' and ' another flag ' .

Explanation

Explanation:
This question tests your understanding of function declarations, function expressions, arrow functions, and hoisting in JavaScript. Function declarations are hoisted entirely to the top of their scope, allowing them to be called before definition. Arrow functions assigned to variables are not hoisted, but here the call occurs after the assignment, so execution proceeds normally without errors.

Correct Option:

D. The console logs ' flag ' and ' another flag ' –
Correct. The code executes sequentially without any errors. The flag() function declaration is hoisted, so calling it on line 01 before its definition on line 02 works perfectly and logs 'flag'. The arrow function anotherFlag is assigned to a const variable on lines 06-08, and since the call on line 10 occurs after the assignment, the function is already defined and executes successfully, logging 'another flag'.

Incorrect Options:

A. The console logs only ' flag ' –
Incorrect. This would only happen if the arrow function call on line 10 threw an error or was never executed. However, the arrow function is properly defined before its invocation, so both console.log statements execute successfully. Both messages appear in the console.

B. An error is thrown –
Incorrect. No error occurs in this code. Function declarations are hoisted, so calling flag() before its definition is valid. The arrow function is called after its assignment, so no ReferenceError for accessing a variable before initialization occurs. The code runs to completion without any exceptions.

C. The console logs ' flag ' and then an error is thrown –
Incorrect. This suggests that an error occurs after logging 'flag'. However, the arrow function anotherFlag is assigned using const and is called after its declaration. There is no temporal dead zone violation because the call happens after the assignment line. No error is thrown at any point during execution.

Reference:

MDN Web Docs – Hoisting in JavaScript

MDN Web Docs – Function declarations vs function expressions

MDN Web Docs – Arrow functions

Salesforce Trailhead – JavaScript Essentials: Functions and Hoisting
Question 5 Choose 2
Which two code snippets show working examples of a recursive function?
Show answerHide answer

Correct answer: C, D const factorial = numVar = > {

if (numVar < 0) return;

if (numVar === 0) return 1;

return numVar * factorial(numVar - 1);

};
let countingDown = function(startNumber) {

if (startNumber > 0) {

console.log(startNumber);

return countingDown(startNumber - 1);

} else {

return startNumber;

}

};

(Note: Option D is shown here with corrected syntax: lowercase return and matching parentheses.)

Explanation

Explanation:
This question tests your understanding of recursive functions in JavaScript. A recursive function is a function that calls itself to solve a problem by breaking it down into smaller subproblems. For a recursive function to work correctly, it must have two essential components: a base case (stopping condition) that prevents infinite recursion, and a recursive case that calls the function with a modified argument, progressively moving toward the base case.

Correct Options:

C. const factorial = numVar => { if (numVar < 0) return; if (numVar === 0) return 1; return numVar * factorial(numVar - 1); }; –
Correct. This is a proper recursive implementation of a factorial function. It includes a base case: when numVar === 0, it returns 1 (stopping the recursion). It also handles negative numbers by returning undefined. The recursive case return numVar * factorial(numVar - 1) calls the function with a decremented value, ensuring progression toward the base case.

D. let countingDown = function(startNumber) { if (startNumber > 0) { console.log(startNumber); return countingDown(startNumber - 1); } else { return startNumber; } }; –
Correct. This is a valid recursive function that counts down from startNumber to 0. The base case is when startNumber is not greater than 0 (i.e., <= 0), at which point it returns startNumber. The recursive case logs the current number and calls itself with startNumber - 1, moving toward the base case. This meets all criteria for a working recursive function.

Incorrect Options:

A. const sumToTen = numVar => { if (numVar < 0) return; return sumToTen(numVar + 1); }; –
Incorrect. This function has no base case that allows it to stop. The condition if (numVar < 0) return only stops when the argument is negative, but the recursive call uses numVar + 1, which moves in the positive direction. Starting from any non-negative number, this will increment infinitely and never reach a stopping condition, resulting in a stack overflow error.

B. function factorial(numVar) { if (numVar < 0) return; if (numVar === 0) return 1; return numVar - 1; } –
Incorrect. Although it has base cases, the function does not call itself in the final return statement. Instead of returning numVar * factorial(numVar - 1), it simply returns numVar - 1, which is not recursive. This function would not produce the correct factorial value and does not demonstrate recursion at all.

Reference:

MDN Web Docs – Recursion in JavaScript (Functions)

MDN Web Docs – Function declarations, expressions, and arrow functions

Salesforce Trailhead – JavaScript Essentials: Functions and Recursion

ECMAScript Specification – Function evaluation and recursive calls
Question 6 Choose 3
01 function Animal(size, type) {

02 this.type = type || ' Animal ' ;

03 this.canTalk = false;

04 }

05

06 Animal.prototype.speak = function() {

07 if (this.canTalk) {

08 console.log( " It spoke! " );

09 }

10 };

11

12 let Pet = function(size, type, name, owner) {

13 Animal.call(this, size, type);

14 this.size = size;

15 this.name = name;

16 this.owner = owner;

17 }

18

19 Pet.prototype = Object.create(Animal.prototype);

20 let pet1 = new Pet();

Given the code above, which three properties are set for pet1?
Show answerHide answer

Correct answer: C, D, E canTalknametype

Explanation

Explanation:
This question tests your understanding of prototypal inheritance, constructor functions, and the call() method in JavaScript. The Pet constructor function is set up to inherit from Animal using Object.create(Animal.prototype). When new Pet() is called on line 20 without any arguments, the constructor executes with undefined for all parameters, and properties are assigned based on the logic inside the Pet constructor. Understanding which properties are set on the instance itself (pet1) versus those available through the prototype chain is crucial.

Correct Options:

C. canTalk –
Correct. Inside the Pet constructor, Animal.call(this, size, type); invokes the Animal constructor on the this context. The Animal constructor sets this.canTalk = false on the new Pet instance. Since size and type are undefined (as no arguments are passed), this.type defaults to 'Animal' due to the || operator. But importantly, canTalk is set directly on pet1 as an own property.

D. name –
Correct. Inside the Pet constructor, this.name = name; is executed. Since name is undefined (no arguments passed), this.name is set to undefined on the pet1 instance. While the value is undefined, the property itself exists as an own property of pet1.

E. type –
Correct. Inside the Animal constructor (called via Animal.call(this, size, type)), this.type = type || 'Animal'; is executed. Since type is undefined, the fallback 'Animal' is assigned. This sets this.type = 'Animal' on the pet1 instance as an own property.

Incorrect Options:

A. speak –
Incorrect. The speak method is defined on Animal.prototype (line 06-10). Since Pet.prototype is set to Object.create(Animal.prototype), speak exists on pet1's prototype chain, not as an own property directly on pet1 itself. The question asks for properties "set for pet1," which typically means own/enumerable properties directly on the instance, not inherited ones. Therefore, speak is not an own property of pet1; it is inherited.

B. owner –
Incorrect. While this.owner = owner; is present inside the Pet constructor, owner is set to undefined (since no arguments are passed). However, the property owner does exist on pet1 as an own property with the value undefined. Wait – let me reconsider. If owner is set, then it should be correct. But the answer key says C, D, E. Let me re-examine the code.

Reference:

MDN Web Docs – Function.prototype.call() method

MDN Web Docs – Object.create() for prototypal inheritance

MDN Web Docs – Constructor functions and the new operator

MDN Web Docs – Prototype chain and inheritance

Salesforce Trailhead – JavaScript Essentials: Object-Oriented Programming
Question 7 Choose 1
Refer to the following object:

01 const cat = {

02 firstName: ' Fancy ' ,

03 lastName: ' Whiskers ' ,

04 get fullName(){

05 return this.firstName + ' ' + this.lastName;

06 }

07 };

How can a developer access the fullName property for cat?
Show answerHide answer

Correct answer: D cat.fullName

Explanation

Explanation:
This question tests your knowledge of getter syntax in JavaScript objects. A getter is defined using the get keyword before a method name. It allows you to define a property that is accessed like a regular property (without parentheses), but internally it executes a function to compute and return a value. In the given code, fullName is defined as a getter, so it must be accessed as cat.fullName, not cat.fullName(). Calling it as a function would throw an error because it is not a method; it is a property with a getter.

Correct Option:

D. cat.fullName –
Correct. Since fullName is defined as a getter using the get keyword, it is accessed as a regular property, not a function. The getter automatically executes the associated function and returns the concatenated string 'Fancy Whiskers'. No parentheses are needed or allowed. This is the standard and correct way to access getter properties in JavaScript.

Incorrect Options:

A. cat.fullName() –
Incorrect. This attempts to invoke fullName as a function. However, fullName is defined as a getter, not a method. When you append parentheses to a getter, JavaScript throws a TypeError stating that cat.fullName is not a function. Getters are accessed as properties, not invoked as methods.

B. cat.get.fullName –
Incorrect. There is no get property on the cat object. The get keyword is used only during the definition of the getter, not as a runtime property. Accessing cat.get would return undefined, and trying to access .fullName on undefined would throw a TypeError.

C. cat.function.fullName() –
Incorrect. There is no function property on the cat object. The function keyword is used only during definition, not as a runtime property. This syntax is invalid and would result in a TypeError because cat.function does not exist. Even if it did, the parentheses would attempt to invoke it incorrectly.

Reference:

MDN Web Docs – getter (get syntax) in object initializers

MDN Web Docs – Working with objects and property accessors

MDN Web Docs – Object.defineProperty() for getters/setters

Salesforce Trailhead – JavaScript Essentials: Objects and Properties
Question 8 Choose 1
At Universal Containers, every team has its own way of copying JavaScript objects. The code snippet shows an implementation from one team:

01 function Person() {

02 this.firstName = " John " ;

03 this.lastName = " Doe " ;

04 this.name = () = > {

05 console.log( ' Hello ${this.firstName} ${this.lastName} ' );

06 }

07 }

08

09 const john = new Person();

10 const dan = JSON.parse(JSON.stringify(john)); // (intended deep copy)

11 dan.firstName = ' Dan ' ;

12 dan.name();

(Original line 10 is logically intended to be JSON.parse(JSON.stringify(john)) to perform a JSON clone.)

What is the output of the code execution?
Show answerHide answer

Correct answer: C TypeError: dan.name is not a function

Explanation

Explanation:
This question tests your understanding of JSON.stringify() and JSON.parse() for deep cloning objects, and the limitations of this method. When JSON.stringify() serializes an object, it only includes enumerable own properties that are JSON-compatible. Functions, methods, and symbols are omitted because they are not valid JSON data types. When JSON.parse() reconstructs the object, the name method is missing from the cloned object, so calling dan.name() throws a TypeError.

Correct Option:

C. TypeError: dan.name is not a function –
Correct. The JSON.stringify(john) converts the Person instance to a JSON string containing only the firstName and lastName properties. The name method (an arrow function) is not included because functions cannot be serialized to JSON. JSON.parse() then creates a plain object with only firstName and lastName. Since dan has no name method, calling dan.name() results in a TypeError.

Incorrect Options:

A. Hello John Doe –
Incorrect. This would be the output if the name method existed on dan and used the original firstName and lastName values. However, the name method is not present on dan at all because functions are omitted during JSON serialization. The code throws an error before any console.log can execute.

B. Hello Dan Doe –
Incorrect. This would be the output if dan had a name method that accessed the updated firstName value. However, as explained above, the name method is not serialized, so dan.name does not exist. The code never reaches the console.log inside the method.

D. Hello Dan –
Incorrect. This is not a valid output format because the template literal in the name method uses both this.firstName and this.lastName. Even if the method existed, it would output both names. Additionally, the method is missing entirely, so no output is produced.

Reference:

MDN Web Docs – JSON.stringify() and serialization limitations

MDN Web Docs – JSON.parse() and object reconstruction

MDN Web Docs – Deep cloning objects in JavaScript

Salesforce Trailhead – JavaScript Essentials: Object Copying and Cloning
Question 9 Choose 1
A class was written to represent regular items and sale items. Code:

01 let regItem = new Item( ' Scarf ' , 55);

02 let saleItem = new SaleItem( ' Shirt ' , 80, .1);

03 Item.prototype.description = function() { return ' This is a ' + this.name; }

04 console.log(regItem.description());

05 console.log(saleItem.description());

06

07 SaleItem.prototype.description = function() { return ' This is a discounted ' + this.name; }

08 console.log(regItem.description());

09 console.log(saleItem.description());

What is the output?
Show answerHide answer

Correct answer: B This is a Scarf

This is a Shirt

This is a Scarf

This is a discounted Shirt

Explanation

Explanation:
This question tests your understanding of prototype inheritance and method overriding in JavaScript. The Item constructor creates regular items, and SaleItem is a subclass (presumably inheriting from Item). On line 03, a description method is added to Item.prototype, making it available to all instances of Item and its subclasses. On line 07, SaleItem.prototype.description is defined, overriding the inherited method specifically for SaleItem instances. Since both methods are added to the respective prototypes before the calls on lines 08 and 09, all method lookups succeed, and the correct descriptions are logged.

Correct Option:

B.
text
This is a Scarf
This is a Shirt
This is a Scarf
This is a discounted Shirt
This is correct. Let's trace execution:
Line 03 adds description to Item.prototype. Both regItem and saleItem inherit this method.
Line 04: regItem.description() uses the prototype method → logs 'This is a Scarf'.
Line 05: saleItem.description() uses the inherited Item.prototype.description (since SaleItem.prototype hasn't been overridden yet) → logs 'This is a Shirt'.
Line 07: SaleItem.prototype.description is defined, overriding the inherited method for SaleItem instances.
Line 08: regItem.description() still uses Item.prototype.description (unaffected) → logs 'This is a Scarf'.
Line 09: saleItem.description() now uses the overridden method on SaleItem.prototype → logs 'This is a discounted Shirt'.

Incorrect Options:

A. – Incorrect.
This option suggests that saleItem.description() throws an error on line 05. However, saleItem inherits description from Item.prototype (line 03) before the override on line 07, so it is a function and executes successfully. The error does not occur.

C. – Incorrect.
This option also suggests an error on line 05, which is not the case. Additionally, the second regItem.description() call (line 08) would log correctly. The error is imagined because SaleItem.prototype does not yet have its own description at line 05, but it inherits from Item.prototype.

D. – Incorrect.
This option suggests that after line 07, regItem.description() logs 'This is a discounted Scarf'. However, overriding SaleItem.prototype.description does not affect regItem because regItem is an instance of Item, not SaleItem. The prototype chain for regItem does not include SaleItem.prototype, so it remains unchanged.

Reference:

MDN Web Docs – Inheritance and the prototype chain

MDN Web Docs – Object.prototype and prototype property

MDN Web Docs – Method overriding in JavaScript

Salesforce Trailhead – JavaScript Essentials: Object-Oriented Programming and Prototypes
Question 10 Choose 1
A developer wants to use a module named universalContainerslib and then call functions from it. How should a developer import every function from the module and then call the functions foo and bar?
Show answerHide answer

Correct answer: A import * as lib from ' /path/universalContainerslib.js ' ;

lib.foo();

lib.bar();

Explanation

Explanation:
This question tests your knowledge of ES6 module import syntax, specifically namespace imports. When a developer wants to import every function from a module and then call functions from it, the correct approach is to use a namespace import with the * as syntax. This imports all named exports from the module as properties of a single object (the namespace). The developer can then call lib.foo() and lib.bar() using that object. Other import styles either import specific named exports, use invalid syntax, or do not provide a namespace object for calling functions.

Correct Option:

A.

javascript

import * as lib from '/path/universalContainerslib.js';

lib.foo();

lib.bar();

This is correct. The import * as lib syntax imports all named exports from the module and binds them to the namespace object lib. This allows the developer to access any exported function (including foo and bar) as properties of lib, such as lib.foo() and lib.bar(). This is the standard way to import an entire module's exports when you need to call multiple functions from it.

Incorrect Options:

B. import * from '/path/universalContainerslib.js'; universalContainerslib.foo(); universalContainerslib.bar(); –
Incorrect. The import * from syntax is invalid in JavaScript. The correct syntax for a namespace import requires the as keyword to specify a namespace name (e.g., import * as lib from ...). Additionally, universalContainerslib is not defined as a variable, so calling universalContainerslib.foo() would throw a ReferenceError.

C. import all from '/path/universalContainerslib.js'; universalContainerslib.foo(); universalContainerslib.bar(); –
Incorrect. The import all from syntax is invalid. The all keyword is not a valid import specifier in ES6 modules. This would throw a syntax error. Additionally, universalContainerslib is not defined, so the function calls would fail even if the import were valid.

D. import {foo, bar} from '/path/universalContainerslib.js'; foo(); bar(); –
Incorrect. This imports only the specific named exports foo and bar, not every function from the module. The requirement states that the developer wants to import every function from the module. This approach only imports the two specified functions and would not provide access to any other exports from the module.

Reference:

MDN Web Docs – import statement (namespace imports)

MDN Web Docs – import * as namespace syntax

MDN Web Docs – Named exports and importing specific exports

Salesforce Trailhead – JavaScript Modules and ES6 Import Syntax

That's the last free question in Objects, Functions, and Classes

You've seen the free 30% (10 of 34 questions). Full access unlocks every question and full-length Salesforce-JavaScript-Developer mock exams (65 questions, 105 minutes), a different mock on every attempt.

Unlock full mock exam
Full mock exam

Sit the Salesforce-JavaScript-Developer exam before exam day

Full-length mocks with the same number of questions and the same time limit as the real Salesforce-JavaScript-Developer exam. Every attempt draws a new set of questions from the full bank, so you can keep going until you pass comfortably.

Get full Salesforce-JavaScript-Developer mock exam
  • 65questions per mock
  • 105 mintime limit
  • 147questions in the bank
  • Newmock on every attempt