Infinity
(or -Infinity
) is the value JavaScript will return if you calculate a number outside the largest possible number.
Example
var myNumber = 2;
while (myNumber != Infinity) { // Execute until Infinity
myNumber = myNumber * myNumber;
}
while (myNumber != Infinity) { // Execute until Infinity
myNumber = myNumber * myNumber;
}
Division by 0 (zero) also generates Infinity
:
Example
var x = 2 / 0; // x will be Infinity
var y = -2 / 0; // y will be -Infinity
var y = -2 / 0; // y will be -Infinity
Infinity
is a number: typeof Infinity
returns number
.
Example
typeof Infinity; // returns "number"
Practice Excercise Practice now