Why sum of zero in an Array returns me 1?


I was playing around with a code. Curiously a value is returned when two zero’s were added.

const sum = (...args) => {


  return args.map((a, b) => a + b, 0);



Returns me this [0,1,2]

Is it because the values are in string format?

When i pass values to sum(0,1,2) it returns me [0,2,4]

How is it that when i pass multiple zero’s it returns me values instead of zero’s

you are using map, not reduce

in map a is current element, b the index

Hi @Hisoka. What did you expect to get? You are mapping the arguments to a + b where a is the value and b is the index. You can read more about map and its usage below.