Skip to content

JavaScript negative infinity

  • by

In JavaScript, “Negative Infinity” is a special value that represents a number that is smaller than any other number, including negative numbers. It is denoted by the keyword Number.NEGATIVE_INFINITY.

In JavaScript, the syntax for representing Negative Infinity is by using the keyword Number.NEGATIVE_INFINITY.

var num = Number.NEGATIVE_INFINITY;
console.log(num);  // Output: -Infinity

Negative Infinity is used to representing the result of certain mathematical operations that go beyond the numeric range JavaScript can handle. For example, dividing a non-zero number by zero or subtracting Infinity from a finite number will result in Negative Infinity.

var num = Number.NEGATIVE_INFINITY;
console.log(num);  // Output: -Infinity

console.log(num + 5);  // Output: -Infinity
console.log(num * 10); // Output: -Infinity
console.log(1 / num);  // Output: -0

JavaScript negative infinity example

Simple example code.

// Example 1: Division by Zero
var result = 10 / 0;
console.log(result);  // Output: Infinity

var negativeResult = -10 / 0;
console.log(negativeResult);  // Output: -Infinity

// Example 2: Comparison with Negative Infinity
var num1 = 5;
var num2 = Number.NEGATIVE_INFINITY;

console.log(num1 > num2);   // Output: true
console.log(num2 < num1);   // Output: true

// Example 3: Mathematical Operations with Negative Infinity
var num3 = Number.NEGATIVE_INFINITY;

console.log(num3 + 10);     // Output: -Infinity
console.log(num3 * 5);      // Output: -Infinity
console.log(1 / num3);      // Output: -0

Output:

JavaScript negative infinity

In Example 1, we divide a number by zero, resulting in positive infinity and negative infinity respectively.

In Example 2, we compare a finite number with Negative Infinity using the greater-than and less-than operators. The comparisons evaluate to true because Negative Infinity is considered smaller than any finite number.

In Example 3, we perform mathematical operations such as addition, multiplication, and division involving Negative Infinity. The results of these operations are always Negative Infinity, except when dividing by Negative Infinity, which yields negative zero (-0).

Do comment if you have any doubts or suggestions on this JS infinity topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *