JavaScript Tutorial

# What is infinity in javascript?

In JavaScript, Infinity is a special numeric value that represents positive infinity, which is a concept used to denote a value that is larger than any finite number. It is often used to represent mathematical concepts like division by zero or values that exceed the range of representable numbers.

## Positive Infinity

const positiveInfinity = Infinity; console.log(positiveInfinity); // Output: Infinity console.log(typeof positiveInfinity); // Output: "number" console.log(positiveInfinity + 1); // Output: Infinity (any number added to Infinity is still Infinity) console.log(positiveInfinity * 2); // Output: Infinity (Infinity multiplied by any non-zero number is still Infinity) console.log(positiveInfinity / 0); // Output: Infinity (division by zero)

## Negative Infinity

JavaScript also has a negative infinity value, represented as -Infinity. It's used to denote values that are smaller than any finite number.

const negativeInfinity = -Infinity; console.log(negativeInfinity); // Output: -Infinity console.log(typeof negativeInfinity); // Output: "number" console.log(negativeInfinity - 1); // Output: -Infinity (any number subtracted from -Infinity is still -Infinity) console.log(negativeInfinity / 0); // Output: -Infinity (division by zero)

## Special Cases

### Operations with Infinity

1. Any arithmetic operation with Infinity results in Infinity.
2. Dividing a non-zero finite number by Infinity results in 0.
console.log(Infinity + 1); // Output: Infinity console.log(1 / Infinity); // Output: 0

### Comparisons

1. Infinity is considered greater than any finite number.
2. -Infinity is considered smaller than any finite number.
console.log(Infinity > 1000); // Output: true console.log(-Infinity < -1000); // Output: true

### Type Conversions

1. Trying to convert Infinity to a string results in "Infinity".
2. Infinity is truthy in boolean context.
console.log(String(Infinity)); // Output: "Infinity" console.log(Boolean(Infinity)); // Output: true

### Conclusion

Infinity is a concept used in JavaScript to represent unbounded values, typically used for indicating values that are larger (or smaller, with -Infinity) than any representable finite number. It's essential to understand its behavior, especially when dealing with arithmetic operations and comparisons involving large or unbounded values.