NEGATIVE_INFINITY in JavaScript

Shiv Yadav Oct 12, 2023
NEGATIVE_INFINITY in JavaScript

This article will look at the NEGATIVE_INFINITY value in JavaScript.

NEGATIVE_INFINITY in JavaScript

The NEGATIVE_INFINITY in JavaScript is a constant value lower than any other number (it means no other number is lesser than this value). JavaScript displays the NEGATIVE_INFINITY value as -Infinity.

Some conditions where a number is NEGATIVE_INFINITY are mentioned below.

  1. Any positive value multiplied by NEGATIVE_INFINITY is NEGATIVE_INFINITY.
  2. NEGATIVE_INFINITY divided by any positive value except POSITIVE_INFINITY is NEGATIVE_INFINITY.

Let’s see one example.

function cNumber(sNum) {
  if (sNum === Number.NEGATIVE_INFINITY) {
    return '-Infinity';
  }
  return 'provided number is good';
}

console.log(cNumber(-Number.MAX_VALUE));

Run Code

A function cNumber is created, which takes parameter sNum, and if the sNum is equal to Number.NEGATIVE_INFINITY, it returns -Infinity.

In this program, when we run the code, the if statement doesn’t run because the number is not passed as a parameter and then it returns provided number is good, as you can see in the console by running the above code.

Output:

"provided number is good"

Let’s run the code by providing the number as a parameter and see what it returns.

function cNumber(sNum) {
  if (sNum === Number.NEGATIVE_INFINITY) {
    return '-Infinity';
  }
  return 'provided number is good';
}

console.log(cNumber(-Number.MAX_VALUE * 3));

Run Code

Output:

"-Infinity"
Author: Shiv Yadav
Shiv Yadav avatar Shiv Yadav avatar

Shiv is a self-driven and passionate Machine learning Learner who is innovative in application design, development, testing, and deployment and provides program requirements into sustainable advanced technical solutions through JavaScript, Python, and other programs for continuous improvement of AI technologies.

LinkedIn