Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Negative Infinity
2.1.
Properties of Negative Infinity
2.2.
Code
2.3.
Output 
3.
Positive Infinity
3.1.
Properties of Positive Infinity
3.2.
Code 
3.3.
Output
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Negative and Positive Infinity in JavaScript

Author Aditya Anand
1 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Welcome readers! The value Infinity is needed in many programming problems, and Javascript supports the invaluable Positive and Negative Infinity to help the programmers. In this blog, we will learn about them and their properties and an example code.

Following browsers support Negative and positive Infinity.

  • Google Chrome
  • Mozilla Firefox
  • Apple Safari
  • Opera
  • Internet Explorer/Edge

Negative Infinity

In JavaScript, negative infinity is a constant number used to indicate the lowest possible value. This suggests that no other number is smaller than this one. It can be created with a custom function or by performing an arithmetic operation.

JavaScript shows the NEGATIVE_INFINITY value as -Infinity.

Syntax:

Number.NEGATIVE_INFINITY

Properties of Negative Infinity

Following are the properties of Negative Infinity:

  • When negative infinity is divided by any other number, the result is 0.
  • Negative infinity returns NaN when divided by itself or by positive infinity.
  • When any positive number (other than positive infinity) is divided by negative infinity, the result is negative infinity.
  • Positive infinity is negative infinity divided by any negative number (other than negative infinity).
  • When negative infinity is multiplied by NaN, the outcome is NaN.
  • The sum of NaN and negative infinity equals zero.
  • When two negative infinities are added together, the result is always a positive infinity.
  • Negative infinity is always the product of both positive and negative infinity.

Code

function checkNumber(smallNumber) {
    if (smallNumber === Number.NEGATIVE_INFINITY) {
      return 'Process number as -Infinity';
    }
    return smallNumber;
  }
  console.log(checkNumber(-Number.MAX_VALUE));
  // expected output: -1.7976931348623157e+308
  console.log(checkNumber(-Number.MAX_VALUE * 2));
  // expected output: "Process number as -Infinity"

Output 

You can practice by yourself with the help of Online Javascript Compiler for better understanding.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Positive Infinity

In Javascript, positive infinity is a number that is constant and represents the maximum possible value. It can be created with a custom function or by performing an arithmetic operation.

JavaScript shows the POSITIVE_INFINITY value as Infinity.

Syntax:

Number.POSITIVE_INFINITY

Properties of Positive Infinity

Following are the properties of Positive Infinity:

  • Negative infinity is the product of positive and negative infinity.
  • We get positive 0 when we divide any positive number by positive infinity.
  • When we divide any negative number by positive infinity, we get negative 0 0. When we multiply that by positive infinity, we get NaN.
  • Positive infinity multiplied by NaN equals NaN.
  • When we divide positive infinity by any negative number, we obtain negative infinity (except negative infinity)
  • When we divide positive infinity by any positive number, we get positive infinity (except positive infinity)
  • NaN is the result of dividing positive infinity by either positive or negative infinity.

Code
 

function checkNumber(bigNumber) {
    if (bigNumber === Number.POSITIVE_INFINITY) {
      return 'Process number as Infinity';
    }
    return bigNumber;
  }
 
  console.log(checkNumber(Number.MAX_VALUE));
  // expected output: 1.7976931348623157e+308
  console.log(checkNumber(Number.MAX_VALUE * 2));
  // expected output: Process number as Infinity

Output


Check out this problem - First Missing Positive 

Frequently Asked Questions

  1. What is positive infinity?
    The positive infinity property represents the highest available value.
     
  2. What is the meaning of -Infinity?
    -Infinity is a numeric value that represents negative infinity. Infinity is displayed when the number exceeds the upper limit of floating-point numbers, equal to 1,779693134862315E + 308.
     
  3. What is the value of Nan Multiplied by negative infinity?
    If we multiply negative infinity with NaN, we will get NaN as a result.
     
  4. Why is JavaScript called a scripting language?
    JavaScript is not a programming language in a strict sense. Instead, it is a scripting language because it uses the browser to do the dirty work. If you command an image to be replaced by another one, JavaScript tells the browser to do it.

Conclusion

In this article, we have extensively discussed Negative and Positive Infinity in Javascript.

  • In this blog, we have learned about positive and negative infinity.
  • We have seen the properties of negative and positive infinity.
  • We have seen an example code of both of them.

We hope that this blog has helped you enhance your knowledge regarding Negative and Positive Infinity. If you want to learn more, check out our Javascript Numbers and Javascript Numbers articles. Do upvote our blog to help other ninjas grow.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

 

Previous article
Async and Defer Attributes in Javascript
Next article
Service Workers
Live masterclass