6

I'm using a generator to create objects, like so:

function* Thing() {
  var x = 0;
  while (x < 3) {
    var rules = [{arr: [1, 2, 3]}, {arr: [1]}, {arr: []}];
    yield {
      arrayMinimum: Math.min(...rules[x].arr)
    }
    x++
  }
}

var create = Thing();

console.log(create.next().value)
console.log(create.next().value)
console.log(create.next().value) // { arrayMinimum: Infinity } ???

Why is Math.min(...[]) === Infinity?

Bonus confusion: Math.max(...[]) === -Infinity ???

Kris Molinari
  • 503
  • 6
  • 17
  • [`...` is not an operator!](https://stackoverflow.com/questions/37151966/what-is-spreadelement-in-ecmascript-documentation-is-it-the-same-as-spread-oper/37152508#37152508) (didn't even know we had a tag for that :( ) – Felix Kling Mar 27 '17 at 18:40

1 Answers1

13

This is because the arguments-list specified by (...[]) is the empty list of arguments -- i.e., you are doing Math.min() with no arguments.

The EMCAScript spec for Math.min states:

If no arguments are given, the result is +∞.

(And, unsurprisingly, it has a similar statement for Math.max.)

apsillers
  • 112,806
  • 17
  • 235
  • 239