In Javascript, why is the minimum pool with an expanded Infinity array?

I use a generator to create objects, for example:

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 } ???
Run codeHide result

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

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

+4
source share
1 answer

This is because the argument list specified (...[])is an empty argument list - i.e. you do Math.min()without arguments.

EMCAScript specification for Math.minstates :

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

(And it is not surprising that he is a similar operator forMath.max .)

+9
source

Source: https://habr.com/ru/post/1673340/


All Articles