The positive infinity 'Infinity' and negative infinity '-Infinity' representation of js

In JavaScript, positive and negative infinity are represented by special global properties. They are Infinityand -Infinity.

  • Positive infinity: Infinity
    Positive infinity represents a value that is greater than any real number.
console.log(Infinity); // 输出:Infinity
console.log(10 / 0); // 输出:Infinity
console.log(1 / 0); // 输出:Infinity
  • Negative Infinity: -Infinity
    Negative infinity represents a value that is smaller than any real number.
console.log(-Infinity); // 输出:-Infinity
console.log(-10 / 0); // 输出:-Infinity
console.log(-1 / 0); // 输出:-Infinity

Use positive infinity and negative infinity to handle situations outside JavaScript's numeric range, such as division by zero, numeric overflow, and so on.

Note that positive infinity and negative infinity are both numeric types. They have special properties in mathematical operations, such as multiplying any real number with positive infinity to get positive infinity, multiplying with negative infinity to get negative infinity, adding positive infinity to negative infinity to get NaN (not a number), etc.

console.log(Infinity * 10); // 输出:Infinity
console.log(-Infinity * 10); // 输出:-Infinity
console.log(Infinity + (-Infinity)); // 输出:NaN

Positive infinity and negative infinity are very useful in some mathematical calculations and conditional judgments, and can help deal with special cases and boundary conditions.

Guess you like

Origin blog.csdn.net/qq_41045651/article/details/131598301