The answer can become clear when you try to determine how else you would do this. Here's another candidate solution:
function findMax() {
var i;
var max = arguments[0];
for (i = 1; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
This one seems simpler. You don't need an initial value, taking instead the first value of the arguments.
And it seems to work:
findMax(8, 6, 7, 5, 3, 0, 9, 8, 6, 7, 5); //=> 9
It works even if you only supply a single value:
findMax(42); //=> 42
Can you see where it fails? There is a serious problem with this implementation.
Did you find it?
findMax(); //=> undefined
When you pass it no arguments at all, you get undefined, a non-numerical result.
With the other version, it would return -Infinity. This answer has a numeric type; so the function always returns a number. Having consistent return types makes programming with the system much easier.
And if you really want to understand the basic idea more deeply, imagine a concatenateAllStrings function. You could do this much the same way, adding to your final value each new string your encounter. But you would need to start with something. There's only one reasonable choice: the empty string. That's because for any string s, '' + s //=> s. In your case, you want the value x such that for any number n, n >= x. What's less than every number? Really only -Infinity.
For a still deeper look, you might investigate "folding over a Semigroup".
I've actually used a related fact in interviewing JS developers. This is only once the user has shown herself to be reasonably competent. I ask something like this:
This sounds a bit like a trick question, but the goal is to see how you might try to understand existing systems. It's an odd quirk of Javascript that Math.max() < Math.min(). Can you discuss how max and min might be implemented to make this happen?
And then I guide her through it, offering as many hints as necessary until she has explained it in full. No one has ever known the answer right off. How much guidance the candidate needs, and how well she seems to understand the explanation, helps me determine something useful about the her.