According to MDN, " The rest parameter syntax allows a function to accept an indefinite number of arguments as an array". Basically, if you don’t know how many arguments will be passed to your function you can use rest parameter syntax and it will put all the arguments in an array for you.
The reduce method can do a few different things, but in this case it’s just adding up all the numbers in the array to get the total.
oh my god, I thought “(a, b)” means, the function only accept two value from …args, but now that I read the article you provided. It actually applied the function to each values inside the array. thanks you so much!
But I still have a question, if you don’t mind.
do you know what that 0 is, at the end return args.reduce((a, b) => a + b, 0);
The 0 is the ‘initial value’. In this case it doesn’t do anything because essentially it just means ‘add up all the values starting from 0’, which is what the default is anyway. You can remove it and the function will still work the same. It’s optional to have it.
However try changing it to 5 or 10 and you will get different results. If you change it to 5, then you are saying ‘add up all the values starting from 5’.
Check out the link to the reduce method that @sitek94 posted. They have a nice example of that in the demo on line 9.