How -Infinity is converted to String in JavaScript?


We all know that string is one of the primitive data types in JavaScript. JavaScript allows us to do some operations on strings like finding characters in the string, changing all characters into lowercase or into uppercase, concatenating with other strings, replacing with other strings, searching character(s) or word(s) from string, etc.

We denote string in single quotations or in double quotations. typeof string variable will be a string.

Syntax

Syntax for String() method is, like

String(value) 

value is required. value is JavaScript value. String() method returns a value i.e. as a string. String() method introduced in ES1 and it is fully supported by all browsers.

Steps to convert -Infinity to String

STEP 1 - Declare a variable and assign -Infinity to it.

STEP 2 - Convert -Infinity to string using the String() method.

STEP 3 - Display the result.

Let's look at examples for converting value into string.

Example

Converting -Infinity to String

In the below example, we convert "–Infinity" to string using the String() method in JavaScript.

<!DOCTYPE html>
<html>
<body>
   <h2>Converting - Infinfity to String</h2>
   <p>value and typeof value :
      <span id="value"></span>
   </p>
   <p>value and typeof value :
      <span id="result"></span>
   </p>
   <script>
      var value = - Infinity;
      document.getElementById('value').innerHTML = value + " and " + typeof value;
      var str = String(value);
      document.getElementById('result').innerHTML = str + " and " + typeof str;
   </script>
</body>
</html>

Here, we can see after the String() method, the typeof value changed from number to string.

Example

Converting Infinity to String

We can also convert "Infinity" to string. Let's look into an example

<!DOCTYPE html>
<html>
<body>
   <h2>Converting Infinity to String</h2>
   <p>value and typeof value :
      <span id="value"></span>
   </p>
   <p>value and typeof value :
      <span id="result"></span>
   </p>
   <script>
      var value = Infinity;
      document.getElementById('value').innerHTML = value + " and " + typeof value;
      value = String(value);
      document.getElementById('result').innerHTML = value + " and " + typeof value;
   </script>
</body>
</html>

Example

Using the toString() method

Note that the String() method and toString() method returns the same result for any value. Here, toString() is one of the methods of JS string. Let's look at an example with toString() also.

<!DOCTYPE html>
<html>
<body>
   <h2>Converting -Infinity to String</h2>
   <p>value and typeof value :
      <span id="value"></span>
   </p>
   <p>value and typeof value :
      <span id="result"></span>
   </p>
   <script>
      var value = -Infinity;
      document.getElementById('value').innerHTML = value + " and " + typeof value;
      value = value.toString();
      document.getElementById('result').innerHTML = value + " and " + typeof value;
   </script>
</body>
</html>

In this tutorial, we have seen converting values -Infinity into String in JavaScript. Hope this gives some knowledge on converting -Infinity into string.

Updated on: 08-Dec-2022

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements