Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript, why is the minimum of an empty spread array Infinity?

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 ???

like image 947
Kris Molinari Avatar asked Mar 27 '17 18:03

Kris Molinari


1 Answers

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.)

like image 173
apsillers Avatar answered Oct 19 '22 11:10

apsillers