What it means , 0 in JS Method

I was doing the first HackerRank challenge called “Simple Array Sum” and it was very clear for me that i have to use .reduce to sum all the values in a simple array. I create this:

(ar is the parameter)
let ar = [1, 2, 3, 4, 10, 11];

    let newArr = ar.reduce((x, y) => { return x + y; });

Anyway, it doesn’t work because i was sending the solution to the console.
THEN i google it the “correct” solution and i find this:

    let array = ar.reduce((acc, current) => acc += current, 0 );

can someone explain me what it means that ,0 ?

Hello there,

I will leave you with this, and recommend you read it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

If you have any further specific questions, then return, and we can try to help.

The “correct” solution is wrong because it mutates the accumulator; though it doesn’t really matter here since it’s only mutating the local and returning its new value. Your solution should have been just fine though.

The 0 at the end is the starting accumulator value. If you leave it off, it will start with the first element of the array. If the array is empty, it will error, whereas if you give it a start value, it will return the start value instead. Rule of thumb is if you can use a starting value, always do so.