- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How Is Infinity converted to String in JavaScript?
This tutorial will teach us how to convert the infinity to string in JavaScript. The Infinity is the reserved keyword in JavaScript and demonstrates the concept of the infinity number of mathematics. It is a global variable, and we can access it anywhere in our program.
Here, we have two approaches to converting the infinity number to a string in JavaScript.
Using the String() Constructor
Using the toString() Method
Using the String() Constructor
The JavaScript contains the class for the variable of every data type and its related method. It also contains the string class and which is the String() constructor. When we pass any values to the String() constructor, it creates the variable of the string type and returns the reference of it, and in the same way, we can convert the Infinity to string.
Syntax
Following is the syntax to convert the Infinity to string using String() constructor −
let val = Infinity; let str = String(val);
Parameters
val − It is can be Infinity or -Infinity to convert into the string.
Example
In the example below, we have declared the variables and assigned the Infinity value to them. We have passed that variable to the parameter of the String() constructor to convert it to the string.
<html> <head> </head> <body> <h2> Converting infinity to string in JavaScript. </h2> <h4> Using the <i> String() </i> constructor to convert infinity to string. </h4> <p id = "number"> </p> <script> let number = document.getElementById("number"); let val = Infinity; let str = String(val); number.innerHTML += "String: " + str + "<br/>"; number.innerHTML += "Type of " + str + " is : " + typeof str; </script> </body> </html>
Using the toString() Method
In JavaScript, the toString() method is the most popular for converting any variable value to a string. It is also a String class method, which we can invoke by the reference of any variable.
Syntax
Following is the syntax to use the convert Infinity to using toString() method −
let value = Infinity; let str = value.toString();
Parameters
value − It is a variable that contains the Infinity value, and on the reference of that variable, we are calling the toString() method.
Example
In the example below, we have created two variables and assigned Infinity and -Infinity values to them, respectively. After that, we called the toString() method and converted both variables to strings by taking the reference of those variables.
Also, we have checked the type of the converted value using the typeof operator.
<html> <head> </head> <body> <h2> Converting infinity to string in JavaScript. </h2> <h4> Using the <i> toString() </i> constructor to convert infinity to string. </h4> <p id = "number"> </p> <script> let number = document.getElementById("number"); let val = Infinity; let str = val.toString(); number.innerHTML += "String: " + str + "<br/>"; number.innerHTML += "Type of " + str + " is : " + typeof str + "<br/>"; str = (-Infinity).toString(); number.innerHTML += " String: " + str + "<br/>"; </script> </body> </html>
Users can see that the above output shows that type of Infinity is a string, which means Infinity is converted to a string.
We have used the String() constructor and toString() method to achieve our goal in this tutorial. However, users can use the ternary operator to convert the Infinity to string.
- Related Articles
- How -Infinity is converted to String in JavaScript?
- How Is Infinity converted to Number in JavaScript?
- How -Infinity is converted to Number in JavaScript?
- How -Infinity is converted to Boolean in JavaScript?
- How Is Infinity converted to Boolean in JavaScript?
- How [ ] is converted to String in JavaScript?
- How is NaN converted to String in JavaScript?
- How null is converted to String in JavaScript?
- What will happen when { } is converted to String in JavaScript?
- How [ ] is converted to Number in JavaScript?
- How [ ] is converted to Boolean in JavaScript?
- How is NaN converted to Number in JavaScript?
- How null is converted to Number in JavaScript?
- How null is converted to Boolean in JavaScript?
- How is NaN converted to Boolean in JavaScript?
