Not unless we consider the dictionary meaning of the words, and no they are not built into JavaScript.
Parameters are locally defined variables (in function scope). We declare them arbitrarily, but in keeping with common sense use terms that describe what the variable references, or its purpose.
The above callback is kept going until the object array is reduced to a single value. The array itself is unchanged. Put in simple terms,
Array.reduce((a, b) => a + b))
In a sense we can think of a as being the first term in the array, and b as the last. The last term is popped off the array and added to the first term, and the new value replaces the first term. This process is repeated until there is only one term, which is the resulting value of the method.
The heavy lifting is done by the reduce method, which has a built-in iterator for stepping through the array object.
Consider this mockup of the possible code behind the method…
function reduce(array) {
var clone = array.slice(); // a copy so the original is untouched
while (clone.length > 1) {
clone[0] += clone.pop();
}
return clone[0]
}
array = [1,1,2,3,5,8,13,21,34,55]
console.log(reduce(array)) // 143
Thank you! That was a great explanation and now I feel like I actually understand what’s going on in the example they used for teaching the reduce() method.
const newSum = newNumbers.reduce((accumulator, currentValue) => {
console.log('The value of accumulator: ', accumulator);
console.log('The value of currentValue: ', currentValue);
return accumulator + currentValue;
}, 10);
console.log(newSum);
RETURNS THIS…
The value of accumulator: 10
The value of currentValue: 1
The value of accumulator: 11
The value of currentValue: 3
The value of accumulator: 14
The value of currentValue: 5
The value of accumulator: 19
The value of currentValue: 7
26
BUT THE ACCUMULATOR WAS ONLY DECLARED WITHIN THE CALLBACK FUNCTION, so how can it be that 10, which was only used as an argument outside of the callback function, can somehow equate to the accumulator???
The reduce method permits us to specify an initial value to accumulate from. That is the second argument. If omitted, the value is defined by the type of data in the array. It could be an empty string or zero (for addition) or one (for multiplication), depending whether working with character data or numbers. The initial value is loaded into the accumulator and the process of winding down the array begins, one value at a time computed with the accumulator value.
join() is a form of reducing an array to form a string.
a = 'abcdefghijklmnopqrstuvwxyz'
"abcdefghijklmnopqrstuvwxyz"
a.length
26
b = a.split('')
(26) ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
c = b.reduce((u,v) => u + v, "$ ")
"$ abcdefghijklmnopqrstuvwxyz"
c = b.reduce((u,v) => u + v)
"abcdefghijklmnopqrstuvwxyz"
a = '1234567'
b = a.split('').reduce((u, v) => +u + +v)
28
c = a.split('').reduce((u, v) => +u * +v)
5040
Since you do not provide an initial value (second argument) in your call to reduce, the first element (index 0) in the array is passed as the initial value of accumulator the first time reduce calls your callback. In addition, the second element (index 1) of the array is passed as the second argument to the callback the first time the callback is called.
After the first call to the callback, each subsequent call receives the value returned from the previous call as the first argument, and the next element in the array (continuing with the element at index 2) as the second argument.
So here are the calls that reduce performs on your callback, where I’ve given your callback the name callback as the name that the reduce method might use for it internally:
Because your callback ignores the second argument, there are only 2 things that affect the result: (1) the first element in the array, and (2) the length of the array.
If the array is empty, you’ll get an exception because you don’t supply a second argument to reduce. Otherwise, the result is numbers[0] * 2 ** (numbers.length - 1)
const newNumbers = [1, 3, 5, 7];
const newSum = newNumbers.reduce((accumulator, currentValue)=> {
console.log('The value of accumulator: ', accumulator);
console.log('The value of currentValue: ', currentValue);
console.log(accumulator);
return accumulator + currentValue;
console.log(accumulator);
});
console.log(newSum, 'Outside function');
Inside the function newSum, nothing will log to console if the return is set first. Why is that? Shouldn’t it just not display the first couple of numbers? Why does it totally disrupt it? To see an example, the console.log(accumulator) will show what the accumulator is up until the last one. Why won’t it display like this?
NEVERMIND lol:
The return statement ends function execution and specifies a value to be returned to the function caller.
const newNumbers = [1, 3, 5, 7];
const newSum = newNumbers.reduce((accumulator, currentValue) => {
console.log('The value of accumulator: ', accumulator);
console.log('The value of currentValue: ', currentValue);
return accumulator + currentValue;
});
// BEFORE THE CONSOLE.LOG()
//The value of accumulator: 1
//The value of currentValue: 3
//The value of accumulator: 4
//The value of currentValue: 5
//The value of accumulator: 9
//The value of currentValue: 7
//AFTER THE CONSOLE.LOG()
//The value of accumulator: 1
//The value of currentValue: 3
//The value of accumulator: 4
//The value of currentValue: 5
//The value of accumulator: 9
//The value of currentValue: 7
//16
console.log(newSum);
Before the console.log, why does all of that “value of” appear? I thought you had to actually call the function to make the stuff appear??? If that is true, then the 16 should appear before the console.log, and when you log it, it should appear again… I am confused…
EDIT: Also, when I called the function newSum(), an error appeared.
All right, the .reduce method cannot be used with a function expression.
Why? Because it requires more than one parameter for the call, and we all know you don’t pass parameters to a callback function, just the name of it. If the reduce now requires TWO parameters, where does the callback reference go?
I’m not comfortable using the fat arrow approach to define a callback function. It’s messy and does not clearly show the logic.
Update: Okay, I ignored the warning message and got it to work, according to the console.log() output, but really, it should still allow this. Function expressions have not been depreciated yet so this code is right.