Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Salesforce JS-Dev-101 Exam - Topic 7 Question 9 Discussion

Posted by Emily on May 28, 2026 at 01:12 PM

Refer to the code below:

01 const myFunction = arr => {

02 return arr.reduce((result, current) => {

03 return result + current;

04 }, 10);

05 }

What is the output of this function when called with an empty array?

Suggested Answer: D

We call:

myFunction([]);

Inside:

arr.reduce((result, current) => {

return result + current;

}, 10);

Key points about Array.prototype.reduce:

Signature: array.reduce(callback, initialValue)

If initialValue is provided and the array is empty, reduce:

Does not call the callback at all.

Simply returns initialValue.

Here:

arr is [] (empty).

initialValue is 10.

So:

No iterations of the callback happen (no elements to process).

The return value is the initial value: 10.

So the actual output is:

myFunction([]) === 10;

Among the options:

A: 0 -- incorrect, because the initial value is 10, not 0.

B: Throws an error -- reduce throws only if the array is empty and there is no initialValue. Here we have an initial value, so no error.

C: NaN -- there is no arithmetic with undefined or invalid values; we just return 10.

D: Returns 5 -- the numeric value given is wrong; the correct value is 10.

Given the logic, the correct conceptual result is 10. The option text ''Returns 5'' is almost certainly a typo for ''Returns 10''. Since the letter that is intended to represent the correct behavior is D, we keep:

Answe r: D

Study Guide / Concept Reference (no links):

Array.prototype.reduce behavior with initialValue

Behavior of reduce on an empty array with and without initialValue

Return value when no iterations run


Get Premium JS-Dev-101 Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
17 comments · newest first
CA
Carylon 1 month ago
I hope they don't trick us with that typo!
ER
Erasmo 1 month ago
I feel confident about this one.
LO
Louvenia 1 month ago
Definitely not NaN, that's misleading.
RH
Rhea 2 months ago
I agree, empty array means initial value is used.
AR
Argelia 2 months ago
It starts with 10, so it should return 10.
TA
Tamekia 2 months ago
Option A is wrong, it won't return 0.
SH
Sharita 2 months ago
Yeah, because of the initial value in reduce.
HE
Hester 2 months ago
I think it returns 10.
RE
Regenia 2 months ago
Definitely not an error, it's a valid function!
MA
Malika 3 months ago
Yup, the reduce method starts with the initial value.
YU
Yuki 3 months ago
Wait, are you sure? I thought it would be 0.
AL
Alexis 3 months ago
Totally agree, the initial value is 10!
TE
Tenesha 3 months ago
It returns 10 when called with an empty array.
EA
Earlean 3 months ago
I’m confused because I thought reduce would throw an error if there were no elements, but I guess the initial value prevents that?
EN
Ena 4 months ago
I feel like I saw a similar question where the initial value affected the output. Could it be that it returns 10?
WI
Wilbert 4 months ago
I'm not entirely sure, but I remember something about reduce returning the initial value if the array is empty.
WI
Winfred 4 months ago
I think the initial value of 10 in the reduce function means it should return 10 for an empty array, right?
0/2000 characters

Save Cancel