Salesforce JavaScript Developer I Exam-style mock test
Salesforce JavaScript Developer I Practice Exams|Practice test
Full explanations
Exam format
Pass line 65%
33 USD One-time · lifetime access
Enroll →
Sample questions
Example1 · Variables, Types & Collections
A developer at a B2B SaaS billing platform writes a function where a running invoice total is reassigned inside a for loop, and the loop counter must stay confined to the loop block. Which declaration keywords should be used for the mutable total and the block-scoped counter respectively?
✓Declare the total with let because it allows reassignment, and declare the loop counter with let because it keeps the counter scoped to the loop block
Correct. let permits reassignment for the accumulating total, and a let-declared loop counter is block-scoped so it does not leak outside the loop.
・Declare the total with var because it allows reassignment, and declare the loop counter with var so the counter remains available function-wide
Incorrect. var is function-scoped, so the counter would leak beyond the loop block, which is exactly what the requirement forbids.
・Declare the total with const because it allows reassignment, and declare the loop counter with let to confine it to the loop
Incorrect. const cannot be reassigned, so using it for a total that is updated each iteration throws a TypeError.
・Declare the total with let, and declare the loop counter with const since its value never truly changes
Incorrect. The loop counter is reassigned on every iteration, so const would throw on the first increment attempt.
Explanation
let allows reassignment and is block-scoped, satisfying both the mutable total and the loop-confined counter.
Example2 · Objects, Functions & Classes
A developer at a B2B SaaS company builds a makeApiCounter factory that returns an increment function tracking how many requests each tenant has made. Each tenant should get an isolated count that outside code cannot read or reset directly. Which implementation achieves this encapsulation?
✓Store the count in a variable local to makeApiCounter and return an inner function that increments and returns that variable, so the count lives only in the closure.
Correct. A variable declared inside the factory is captured by the returned inner function's closure, giving each returned counter its own private, non-enumerable state that outside code cannot reach.
・Store the count as a property on the global window object keyed by tenant id, and return a function that reads and updates that global property each call.
Incorrect. Putting the count on the global window object makes it fully public and mutable by any script, which defeats the encapsulation requirement.
・Store the count on the returned function object itself as a public property that any caller is free to read, mutate, or set back to zero at will.
Incorrect. Exposing the count as a public property on the function allows any caller to read or reset it, so it is not private.
・Store the count in a module-level variable shared by every tenant and return a function that increments the single shared variable for all of them.
Incorrect. A single module-level variable is shared across all tenants, so counts collide instead of being isolated per tenant.
Explanation
A closure over a variable local to the factory is the idiomatic way to create private, per-instance state in JavaScript.
Example3 · Browser, DOM & Events
A developer at a B2B SaaS company is building an invoice dashboard and needs to grab the first element that matches the CSS selector ".invoice-row.overdue" so they can highlight it. Which method should they call on the document to return that single matching element?
・document.getElementById("invoice-row overdue") because ID lookups accept any CSS class combination directly as the argument
Incorrect. getElementById expects a single ID value, not a class-based CSS selector, so it cannot match the compound class string shown.
✓document.querySelector(".invoice-row.overdue") because it accepts a CSS selector string and returns the first matching element
Correct. querySelector accepts any valid CSS selector and returns the first element in the document that matches, or null when none match.
・document.getElementsByClassName(".invoice-row.overdue") because it filters using the full compound selector and returns one node
Incorrect. getElementsByClassName expects bare class names without dots or selectors and returns a live collection rather than a single element.
・document.querySelectorAll(".invoice-row.overdue")[0] after collecting every matching element and reading the first index
Incorrect. This works but querySelectorAll returns the entire NodeList of matches, which is unnecessary overhead when only the first match is needed.
Explanation
querySelector is the direct way to retrieve the first element matching a CSS selector; querySelectorAll returns all matches and the getElements* methods do not accept selector syntax.
Example4 · Debugging, Testing & Error Handling
At a healthcare services firm, a function opens a connection to the patient scheduling API, parses the JSON response, and must always close that connection whether the parse succeeds or throws. Which structure guarantees the connection is closed in both outcomes?
✓Place the parsing in try, the error logging in catch, and the connection close in a finally block
Correct. The finally block runs after try or catch regardless of whether an error was thrown, so the connection close happens in every outcome.
・Place the parsing and the connection close together inside a single catch block only
Incorrect. A catch block only executes when an error is thrown, so a successful parse would never reach the close logic placed there.
・Place the parsing in try and duplicate the connection close in both the try and the catch
Incorrect. Duplicating the close in both try and catch is error-prone and still misses cases, which is exactly what finally is designed to centralize.
・Place the parsing in try and the connection close after the try/catch without a finally
Incorrect. Code after try/catch is skipped if an uncaught re-throw propagates, so the close is not guaranteed the way finally guarantees it.
Explanation
finally always executes after the try (and catch, if entered), making it the correct place for mandatory cleanup like closing connections.
Example5 · Asynchronous JavaScript & Server-Side
At MedWell Health Services, a developer builds a function loadPatient(id, callback) that reads a patient record from an API and then invokes the provided callback. To follow the Node.js error-first callback convention the team relies on, what should the signature of the callback they pass be?
✓A function whose first parameter is an error object (null on success) and whose second parameter is the loaded patient data
Correct. The error-first (Node-style) convention passes the error as the first argument (null when none) and the successful data as the second argument.
・A function whose first parameter is the loaded patient data and whose optional second parameter holds any error that occurred
Incorrect. This reverses the convention; the data must not come before the error parameter in an error-first callback.
・A function that takes a single parameter which is a result object containing both data and error fields
Incorrect. Bundling data and error into one result object is a valid pattern but is not the error-first callback convention the team follows.
・A function that takes no parameters and reads the result from a shared outer variable
Incorrect. Reading from a shared outer variable abandons the callback contract and introduces timing and scoping bugs.
Explanation
The error-first callback convention places the error (or null) as the first parameter and the result as the second, letting callers check for failure before using data.
Example6 · Variables, Types & Collections
At a healthcare services firm, a developer logs patient-intake data and runs typeof on several values: a missing insurance ID stored as null, a not-yet-entered allergy field left as undefined, and a numeric copay of 25. What does typeof return for the null insurance ID value?
・It returns the string "undefined", because a null value and an undefined value are treated identically by the typeof operator in JavaScript
Incorrect. typeof null and typeof undefined differ: undefined yields "undefined", but null does not yield that same string here.
✓It returns the string "object", a long-standing behavior of JavaScript that many consider a historical bug in the language
Correct. typeof null returns "object", a well-known legacy quirk retained for backward compatibility in the language.
・It returns the string "null", correctly identifying the value as JavaScript's dedicated null primitive type
Incorrect. There is no "null" return value from typeof; the operator never produces that string for any input.
・It returns the string "number", because null is internally coerced to the numeric value zero before evaluation
Incorrect. typeof does not coerce null to a number, so "number" is not produced for the null insurance ID.
Explanation
typeof null evaluates to "object" due to a historical implementation detail that has been preserved for compatibility; it is one of the common gotchas on this exam.
Example7 · Variables, Types & Collections
On the Northwind Apparel checkout page, a developer logs typeof productPrice where productPrice holds the value null. They expect the console to help them detect an uninitialized price before charging the customer. What does the expression typeof null evaluate to, and why can this mislead the developer?
・It returns "null", which accurately identifies the empty price and lets the check pass as intended
Incorrect. typeof never yields the string "null" for any operand, so this result could not occur in the engine.
✓It returns "object", a long-standing quirk that makes null look like a populated object to the check
Correct. typeof null historically returns "object", a known legacy behavior that can make a null price appear to be an object.
・It returns "undefined", so the price is treated as never having been assigned any value at all
Incorrect. "undefined" is returned only for truly undeclared or unassigned variables, not for an explicit null value.
・It returns "number", because null is internally coerced to zero whenever typeof inspects it
Incorrect. null is not coerced to a number by typeof, so the string "number" is never produced here.
Explanation
typeof null returns "object" due to a long-standing bug preserved for backward compatibility, which can confuse null checks.
Example8 · Variables, Types & Collections
A developer for the Northwind Apparel loyalty widget declares const discountRate = 0.1 at the top of a function. Later in the same function they attempt discountRate = 0.2 after a seasonal promotion begins. What happens when this code runs in strict mode?
・The reassignment silently succeeds and discountRate quietly becomes 0.2 for the rest of the run
Incorrect. A const binding cannot be reassigned, so the new value 0.2 is never applied to the variable.
✓A TypeError is thrown because reassigning a const binding to a new value is not permitted
Correct. Reassigning a const binding throws a TypeError because the binding itself is immutable after initialization.
・The value stays 0.1 and the assignment is ignored without producing any error message whatsoever
Incorrect. The engine does not silently ignore the assignment; it actively raises a TypeError instead.
・A ReferenceError is thrown since const variables are hoisted but remain permanently inaccessible
Incorrect. A ReferenceError would indicate the variable is unreachable, but discountRate is in scope and initialized.
Explanation
Reassigning a const binding throws a TypeError, whereas mutating a const object's properties would be allowed.
Example9 · Variables, Types & Collections
In the Northwind Apparel cart, a quantity value read from a text input is the string "0", and a validation guard runs the comparison "0" == false to decide whether to block an empty order. What is the result of this comparison and the reason behind it?
・false, because a non-empty string and a boolean are considered different types and never match here
Incorrect. Loose equality does convert across types, so differing types alone do not force a false result here.
✓true, because both operands are converted to the number 0 before the loose equality is evaluated
Correct. With ==, both "0" and false are coerced to the number 0, so the comparison evaluates to true.
・true, because any string that contains characters is always treated as truthy during loose equality
Incorrect. Truthiness rules govern boolean contexts, not the stepwise coercion that == performs on these operands.
・false, because loose equality compares the raw string and raw boolean without any numeric conversion
Incorrect. Loose equality explicitly applies numeric conversion to a string-versus-boolean comparison before comparing.
Explanation
Under ==, both the string "0" and false coerce to the number 0, making the comparison true.
Example10 · Variables, Types & Collections
A developer at a financial services firm writes a loan-approval loop that uses var to declare the loop counter inside a function. After the loop finishes, a later line in the same function references that counter and logs its final value without error. Which characteristic of var explains why this reference succeeds?
・var declarations are block-scoped, so the counter is confined to the loop body and discarded immediately after each iteration completes
Incorrect. var is function-scoped rather than block-scoped, so the counter is not confined to the loop body; let or const would provide block scoping.
✓var declarations are hoisted and function-scoped, so the counter remains accessible throughout the entire function after the loop ends
Correct. var declarations are hoisted to the top of the enclosing function and are function-scoped, so the binding survives past the loop and remains readable later in the same function.
・var declarations create a constant binding, so the counter keeps its last value and cannot be reassigned anywhere else in the function
Incorrect. var does not create a constant binding; it is freely reassignable, and the described behavior has nothing to do with immutability.
・var declarations are automatically promoted to the global object, so the counter is shared across every function in the module
Incorrect. A var inside a function is function-scoped, not attached to the global object, so it is not shared across other functions.
Explanation
var is function-scoped and hoisted, meaning a var declared inside a loop remains available anywhere else in the enclosing function after the loop completes.
Example11 · Variables, Types & Collections
While validating a brokerage account record, a developer runs typeof on a field whose value is null, expecting to confirm the field is empty. What value does the typeof operator return for null, and why is this result widely considered surprising?
・It returns "null", which accurately and unambiguously reflects the value and matches most developers' expectations
Incorrect. typeof never returns "null"; there is no such string result for the operator.
・It returns "undefined", because null and undefined are treated as interchangeable primitive values by the operator
Incorrect. typeof null is not "undefined"; typeof undefined is "undefined", but null produces a different result.
✓It returns "object", a long-standing legacy behavior that many developers find misleading since null is a primitive
Correct. typeof null returns "object", a historical quirk of the language kept for backward compatibility, even though null is a primitive.
・It returns "empty", a special placeholder the engine reserves for values that have been explicitly cleared
Incorrect. "empty" is not a value typeof can return; the operator has a fixed, small set of possible string results.
Explanation
typeof null returns "object" due to a legacy implementation detail that has been retained for backward compatibility, which commonly surprises developers.
Example12 · Variables, Types & Collections
A developer at a manufacturing distributor writes a loop that iterates over warehouse pallet IDs and schedules a callback for each ID using setTimeout. Using 'var palletIndex' in the for loop, every callback logs the same final index value instead of the per-iteration value. Which change correctly captures the index separately for each scheduled callback?
・Declare the loop counter with 'const palletIndex' outside the loop and reassign it on every iteration before scheduling the callback
Incorrect. A 'const' binding cannot be reassigned, so reassigning it each iteration throws a TypeError and does not solve the shared-value problem.
✓Replace 'var palletIndex' with 'let palletIndex' so each loop iteration creates a fresh block-scoped binding for the counter
Correct. 'let' is block-scoped and the for loop creates a new binding per iteration, so each callback closes over its own distinct index value.
・Move the 'var palletIndex' declaration to the top of the enclosing function and reset it to zero after each scheduled callback runs
Incorrect. Moving a function-scoped 'var' to the top keeps a single shared binding, so all callbacks still observe the same final value.
・Keep 'var palletIndex' but wrap the setTimeout call in a try/catch block so the counter value is isolated per iteration
Incorrect. A try/catch block affects error handling only and does nothing to create a per-iteration binding for the loop counter.
Explanation
Because 'var' is function-scoped, a single binding is shared by all iterations and closures capture its final value. 'let' is block-scoped and re-bound per iteration, giving each callback its own value.
Example13 · Variables, Types & Collections
In an order-processing module, a developer logs 'typeof shippingFee' where shippingFee was assigned null to signal that the fee has not yet been calculated. The console shows an unexpected result. What value does the typeof expression return for this null variable?
・The string "null", exactly matching the literal value that was assigned to the shippingFee variable
Incorrect. typeof never returns "null"; it reports a type category, and null is not reported with its own literal name.
・The string "undefined", because a variable holding null is treated as never having been initialized
Incorrect. null and undefined are distinct values; a variable explicitly set to null reports its own type, not "undefined".
✓The string "object", a long-standing behavior of typeof when applied to the null value
Correct. Due to a historical quirk in the language, 'typeof null' evaluates to the string "object" even though null is a primitive.
・The string "number", since null is coerced to zero whenever typeof inspects a numeric-context variable
Incorrect. typeof does not coerce null to a number, and inspecting the variable does not place it in a numeric context.
Explanation
'typeof null' returns "object", a well-known legacy behavior. It is distinct from undefined and is not converted to a number by typeof.
Example14 · Variables, Types & Collections
A telecom billing module logs the runtime type of several values. The developer evaluates typeof on the following: a subscriber ID string, an outstanding balance number, a null account pointer, and an array of call records. Which statement correctly describes what typeof returns for these values?
✓typeof returns 'string' for the ID, 'number' for the balance, 'object' for the null pointer, and 'object' for the array of call records.
Correct. typeof null is the historical quirk 'object', and typeof an array is also 'object', so the ID is 'string', the balance is 'number', and both null and the array report 'object'.
・typeof returns 'string' for the ID, 'number' for the balance, 'null' for the null pointer, and 'array' for the array of call records.
Incorrect. typeof never returns 'null' or 'array'; null evaluates to 'object' and arrays also evaluate to 'object' rather than a dedicated 'array' label.
・typeof returns 'text' for the ID, 'number' for the balance, 'undefined' for the null pointer, and 'object' for the array of call records.
Incorrect. typeof on a string is 'string', not 'text', and typeof null is 'object', not 'undefined'.
・typeof returns 'string' for the subscriber ID, 'number' for the outstanding balance, 'null' for the null account pointer, and 'object' for the array of call records.
Incorrect. typeof null is 'object', not 'null'; the rest of this answer is right but that single value makes it wrong.
Explanation
typeof returns 'object' for both null and arrays due to language history; only 'string' and 'number' behave as intuitively expected here.
Example15 · Variables, Types & Collections
In a network provisioning loop, a developer declares a counter with var inside a for block and a modem status with let inside an if block, then tries to read both variables after their blocks close. What is the runtime outcome when the function executes?
・Both the var counter and the let modem status remain accessible after their respective blocks because function-level declarations persist throughout.
Incorrect. let is block-scoped, so the modem status does not persist outside its if block even though var does persist.
✓The var counter is accessible after its loop since var is function-scoped, but reading the let modem status outside its if block throws a ReferenceError.
Correct. var is function-scoped so the counter survives the loop, whereas let is block-scoped so accessing the modem status outside its if block throws a ReferenceError.
・The var counter throws a ReferenceError after its loop, while the let modem status stays accessible across the entire enclosing function body.
Incorrect. This reverses the behavior; var is function-scoped and does not throw, while let is block-scoped and does throw.
・Both the var counter and the let modem status throw a ReferenceError once control leaves the loop and the if block respectively.
Incorrect. var is function-scoped and remains readable, so it does not throw when the loop ends.
Explanation
var is function-scoped and leaks out of blocks, while let (and const) are block-scoped and raise a ReferenceError when accessed outside their declaring block.
▶ Try 10 free questions (graded)