Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How -Infinity is converted to String in JavaScript?
In JavaScript, -Infinity is a special numeric value that represents negative infinity. When we need to convert it to a string, we can use the String() method or the toString() method.
The -Infinity value typically results from mathematical operations like dividing a negative number by zero or when a calculation exceeds the minimum representable number in JavaScript.
Syntax
The String() method converts any JavaScript value to a string representation:
String(value)
Where value is any JavaScript value. The method returns the string representation of that value.
Using String() Method
The most reliable way to convert -Infinity to a string is using the String() method:
<!DOCTYPE html>
<html>
<body>
<h2>Converting -Infinity to String</h2>
<p id="original"></p>
<p id="converted"></p>
<script>
var value = -Infinity;
document.getElementById('original').innerHTML =
"Original: " + value + " (type: " + typeof value + ")";
var stringValue = String(value);
document.getElementById('converted').innerHTML =
"Converted: " + stringValue + " (type: " + typeof stringValue + ")";
</script>
</body>
</html>
Using toString() Method
Alternatively, you can use the toString() method, which produces the same result:
<!DOCTYPE html>
<html>
<body>
<h2>Using toString() Method</h2>
<p id="original"></p>
<p id="converted"></p>
<script>
var value = -Infinity;
document.getElementById('original').innerHTML =
"Original: " + value + " (type: " + typeof value + ")";
var stringValue = value.toString();
document.getElementById('converted').innerHTML =
"Converted: " + stringValue + " (type: " + typeof stringValue + ")";
</script>
</body>
</html>
Converting Positive Infinity
The same methods work for positive Infinity as well:
<!DOCTYPE html>
<html>
<body>
<h2>Converting Infinity to String</h2>
<p id="original"></p>
<p id="converted"></p>
<script>
var value = Infinity;
document.getElementById('original').innerHTML =
"Original: " + value + " (type: " + typeof value + ")";
var stringValue = String(value);
document.getElementById('converted').innerHTML =
"Converted: " + stringValue + " (type: " + typeof stringValue + ")";
</script>
</body>
</html>
Comparison of Methods
| Method | Result for -Infinity | Result for Infinity | Safety |
|---|---|---|---|
String() |
"-Infinity" | "Infinity" | Safe for null/undefined |
toString() |
"-Infinity" | "Infinity" | Throws error on null/undefined |
Practical Example
Here's a practical example showing how -Infinity might occur and how to handle it:
<!DOCTYPE html>
<html>
<body>
<h2>Practical -Infinity Example</h2>
<p id="calculation"></p>
<p id="result"></p>
<script>
var result = -1 / 0; // This creates -Infinity
document.getElementById('calculation').innerHTML =
"Calculation: -1 / 0 = " + result;
var stringResult = String(result);
document.getElementById('result').innerHTML =
"As string: '" + stringResult + "' (length: " + stringResult.length + ")";
</script>
</body>
</html>
Conclusion
Converting -Infinity to a string in JavaScript is straightforward using either String() or toString() methods. The String() method is generally preferred as it safely handles edge cases like null or undefined values.
