Currently, I am diving into Eloquent Javascript and have reached chapter 5, which delves into Higher Order Functions. One of the problems posed in this section involves flattening an array of multiple arrays. The provided solution in the book utilizes an arrow function with parentheses at the end of the line of code. This got me thinking - why are these brackets necessary? When I tested the same code without them, I obtained the same output. My aim is to grasp the significance of these brackets so that I can comprehend the reduce method more effectively and avoid potential pitfalls in the future. Here you can see both versions, yielding identical results.
Despite scouring through various platforms like forums, Stack Overflow, MDN, and w3schools, I could not find a satisfactory explanation.
var arrays = [[1,2,3],[4,5],[6,7],[8,9]];
console.log(arrays.reduce(function(a, b) { return a.concat(b)},[]));
// Brackets----------------------------------------------------^^^
console.log(arrays.reduce(function(a, b) { return a.concat(b)}));
// No Brackets-----------------------------------------------^^^
//result = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Even though both versions yield the same result, there must be a rationale behind including those brackets. I am eager to learn the reason for their presence and understand their purpose. Any insights on this matter would be greatly appreciated!