Fixing the JavaScript “Reduce of empty array with no initial value” Error
When the browser console shows TypeError: Reduce of empty array with no initial value, it means reduce was called on an array with no elements and no initial value. It happens, for example, when you sum the prices of items chosen in a shopping cart and nothing has been selected. Passing an initial value as the second argument to reduce (0 for a sum) removes the error.
In a test environment, I built a page that sums the prices of checkboxes the user has ticked. I clicked the button without selecting anything and got this error. I copied the message exactly as it appeared, and I confirmed that the error goes away after the fix.
The test environment was Ubuntu 24.04 with Node.js 22.23.4. I served the page with Python 3.11's http.server. The browser was Chromium 153, launched with Playwright 1.63.0. Playwright handled the checkbox changes and button clicks, and I read the console messages through the DevTools Protocol. I also took screenshots of the DevTools panel. I did not test Firefox or Safari, did not test use inside frameworks such as React, and did not test reduce on TypedArrays.
- 1. Summing with nothing selected throws an error
- 2. Without an initial value, reduce uses the first element
- 3. Passing 0 as the initial value fixes it
- 4. Arrays of objects give wrong results even when they are not empty
- 5. For a maximum, check for emptiness first if you want a different message
- 6. reduceRight produces the same message
Summing with nothing selected throws an error
The page has three product checkboxes and a button that calculates the total.
<ul id="cart">
<li><label><input type="checkbox" value="1200" data-name="ノート"> ノート 1200円</label></li>
<li><label><input type="checkbox" value="350" data-name="ペン"> ペン 350円</label></li>
<li><label><input type="checkbox" value="800" data-name="付箋"> 付箋 800円</label></li>
</ul>
<button id="calc">選んだ商品の合計</button>
<p id="total">合計:</p>
<script type="module" src="main.js"></script>
In main.js, I turn the values of the checked checkboxes into an array of numbers and add them up with reduce.
const button = document.getElementById('calc');
const total = document.getElementById('total');
button.addEventListener('click', () => {
const checked = document.querySelectorAll('#cart input:checked');
const prices = [...checked].map((input) => Number(input.value));
const sum = prices.reduce((acc, price) => acc + price);
total.textContent = `合計:${sum}円`;
});
Selecting only the pen and clicking the button showed 合計:350円 (Total: 350 yen). Selecting the note and the pen showed 合計:1550円 (Total: 1550 yen). But when I clicked the button with nothing selected, the page stayed at 合計: (Total:) and the following appeared in the Console panel of DevTools:
Uncaught TypeError: Reduce of empty array with no initial value
at Array.reduce (<anonymous>)
at HTMLButtonElement.<anonymous> (main.js:7:22)

The message means "reduce was called on an empty array with no initial value." Since nothing was selected, prices is the empty array [].
The second line, at Array.reduce (<anonymous>), points into reduce itself. The third line points into my code. main.js:7:22 is line 7, column 22 of main.js, which is the position of reduce in prices.reduce (counting the two leading spaces).
Without an initial value, reduce uses the first element
According to MDN's reduce() documentation, when no initial value is given, reduce uses the first element of the array as the starting acc (the accumulated value) and begins calling the function from the second element. An empty array has no first element, so the starting acc cannot be determined and a TypeError is thrown.
MDN's page on this error says it often happens when reduce is called after filter has narrowed the array down to nothing. It also says the error can come from a mistyped selector or from a list whose number of elements is different from what was expected. In a case like the checkboxes, where user actions leave the array empty, you won't see the error while you are testing with something selected.
Passing 0 as the initial value fixes it
I passed 0 as the second argument to the reduce call on line 7.
const sum = prices.reduce((acc, price) => acc + price, 0);
With nothing selected, clicking the button no longer threw an error, and the page showed 合計:0円 (Total: 0 yen). Nothing appeared in the Console panel. With only the pen selected the page showed 合計:350円 (Total: 350 yen), and with the note and the pen it showed 合計:1550円 (Total: 1550 yen). The results with items selected did not change.
According to MDN's reduce() documentation, when an initial value is passed and the array is empty, reduce returns the initial value without calling the function at all. MDN's page on this error advises choosing an initial value that doesn't change the result: 0 for addition, 1 for multiplication, and '' for joining strings.
Arrays of objects give wrong results even when they are not empty
I also tried turning each product into a { name, price } object and adding up price.
const items = [...checked].map((input) => ({ name: input.dataset.name, price: Number(input.value) }));
const sum = items.reduce((acc, item) => acc + item.price);
With nothing selected, the same Uncaught TypeError: Reduce of empty array with no initial value appeared (at main.js:7:21).
That was not the whole problem. Selecting the note and the pen did not throw an error, but the page showed 合計:[object Object]350円 (Total: [object Object]350 yen).
![Japanese page with the note and pen checkboxes ticked; below the total button, whose label is in Japanese and means "Total of selected items," the text reads `合計:[object Object]350円` (Total: [object Object]350 yen). The UI labels are in Japanese.](https://neko-note.org/wp-content/uploads/2026/11/javascript-reduce-of-empty-array-page-object-object.png)
With no initial value, the starting acc was the note object itself. The code then added 350 from item.price to that object. The object was converted to the string [object Object], which was then joined with 350. Selecting only the pen showed 合計:[object Object]円 (Total: [object Object]yen). With one element, the function is never called, so the pen object was returned as is. This matches the edge case for a single element in MDN's reduce() documentation.
Passing 0 as the initial value fixed this case too.
const sum = items.reduce((acc, item) => acc + item.price, 0);
With nothing selected the page showed 合計:0円 (Total: 0 yen). With only the pen it showed 合計:350円 (Total: 350 yen), and with the note and the pen it showed 合計:1550円 (Total: 1550 yen). MDN's reduce() documentation also gives an example of summing values in an array of objects, and it requires an initial value there too.
When the accumulator and the array elements have different shapes (for example, a number and an object), leaving out the initial value breaks the result even if the array is not empty. For calculations like a sum, where an initial value exists that doesn't change the result, always passing one is the safe choice.
For a maximum, check for emptiness first if you want a different message
Next, I found the most expensive selected item.
const max = prices.reduce((a, b) => Math.max(a, b));
Selecting only the pen showed いちばん高い商品:350円 (Most expensive item: 350 yen), and selecting the note and the pen showed いちばん高い商品:1200円 (Most expensive item: 1200 yen). With nothing selected, the same Uncaught TypeError: Reduce of empty array with no initial value appeared as in the total case (at main.js:7:22).
For a maximum, it can be clearer to tell the user that nothing is selected than to show a number. So I used the other fix described in MDN's page on this error: check whether the array is empty before calling reduce, and choose a different message for the empty case.
if (prices.length === 0) {
total.textContent = '商品が選ばれていません';
return;
}
const max = prices.reduce((a, b) => Math.max(a, b));
With nothing selected, clicking the button no longer threw an error, and the page showed 商品が選ばれていません (No items have been selected). The results with items selected were the same as before the change.
reduceRight produces the same message
According to MDN's reduceRight() documentation, reduceRight processes the array from the end, and when no initial value is given it uses the last element as the starting acc. If the array is empty and there is no initial value, it throws the same TypeError as reduce. I wrote a version that joins the names of the selected items in reverse order, without an initial value.
const names = [...checked].map((input) => input.dataset.name);
const text = names.reduceRight((acc, name) => acc + '、' + name);
With nothing selected, the message was the same Reduce of empty array with no initial value. Only the second line changed to at Array.reduceRight (<anonymous>).
Uncaught TypeError: Reduce of empty array with no initial value
at Array.reduceRight (<anonymous>)
at HTMLButtonElement.<anonymous> (main.js:7:22)
The message does not mention reduceRight, so to tell which function threw it, look at the second line. MDN's page on this error says the message is the same in browsers that use V8, in Firefox, and in Safari.




