Strings play a crucial role in representing information that is utilized for displaying, manipulating, and managing operational data. In JavaScript, a number can be transformed into a string through various functions. This article outlines several techniques and methods for converting numbers into strings.
Methods to convert into string in JavaScript
The following ways are used to convert into the string using JavaScript functions.
- Use toString Method
- Use the String function
- Concatenating an Empty String
- Use Template Strings
- Use toLocaleString Method
- Use Lodash_.toString Method
Use toString Method
The Number.Prototype object serves as the foundation for this procedure. It transforms both integer and floating-point numbers into a string representation.
Syntax
The subsequent syntax demonstrates how to transform a number into a string by utilizing the toString method.
Number.toString();
Example
The example below demonstrates the utilization of the toString method for transforming numerical values into string representations.
<!DOCTYPE html>
<html>
<head>
<title> How to convert numbers into string using javascript </title>
</head>
<body style = "background-color:beige;">
<h2> How to convert numbers into string using javascript </h2>
<h4> Use toString() Method to convert number into string </h4>
<script>
let a = 20;
document.write( a.toString()+ "<br>");
document.write( (150).toString()+ "<br>");
document.write( (630).toString()+ "<br>");
document.write( (7).toString(2)+ "<br>");
let a1 = 20.02;
document.write( a1.toString()+ "<br>");
document.write( (6.30).toString()+ "<br>");
document.write( (7.2).toString(2.1)+ "<br>");
</script>
</body>
</html>
Output
The output shows numbers in the string format.
Use the String function
This approach converts a number, whether it is an integer or a floating-point value, into a string format by taking it as input.
Syntax
The subsequent syntax demonstrates how to transform a number into a string utilizing the String function.
String(Object_value);
Example
The subsequent illustration demonstrates the utilization of the String function to transform numerical values into string formats.
<!DOCTYPE html>
<html>
<head>
<title> How to convert numbers into string using javascript </title>
</head>
<body style = "background-color:beige;">
<h2> How to convert numbers into string using javascript </h2>
<h4> Use String() Method to convert number into string </h4>
<script>
let a = 20;
document.write( String(a)+ "<br>");
document.write( String(40)+ "<br>");
document.write( String(51.91)+ "<br>");
let a1 = 20.02;
document.write( String(a1)+ "<br>");
</script>
</body>
</html>
Output
The output shows numbers in the string format.
Concatenating an Empty String
This is the most straightforward approach in JavaScript for transforming an integer or a floating-point number into a string representation.
Syntax
The syntax outlined below can be employed to transform a number into a string by concatenating it with an empty string.
let variableName = "" +data;
Example
The subsequent illustration demonstrates the technique of appending an empty string to transform numbers into string representations.
<!DOCTYPE html>
<html>
<head>
<title> How to convert numbers into string using javascript </title>
</head>
<body style = "background-color:beige;">
<h2> How to convert numbers into string using javascript </h2>
<h4> Use Concatenating an Empty String Method to convert number into string </h4>
<script>
let a = 21;
let op = "" +a;
document.write(op+ "<br>");
document.write( "" +591+ "<br>");
document.write( "" +59.91+ "<br>");
let a1 = 26.12;
let op1 = "" +a1;
document.write( op1+ "<br>");
</script>
</body>
</html>
Output
The output shows numbers in the string format.
Use Template Strings
It is permissible to transform an Integer or Float data type by encapsulating a number within a String.
Syntax
The syntax outlined below demonstrates how to transform numbers into strings utilizing Template Strings.
let variableName = '${data}';
Example
The example below demonstrates the use of the template string approach to transform numerical values into string representations.
<!DOCTYPE html>
<html>
<head>
<title> How to convert numbers into string using javascript </title>
</head>
<body style = "background-color:beige;">
<h2> How to convert numbers into string using javascript </h2>
<h4> Use template string Method to convert numbers into string </h4>
<script>
let a = 51;
let op = `${a}`;
document.write(op+ "<br>");
let a2 = 34;
document.write( `${a2}`+ "<br>");
document.write( `${'76'}`+ "<br>");
document.write( `${67}`+ "<br>");
let a1 = 96.12;
let op1 = `${a1}`;
document.write( op1+ "<br>");
let a3 = 3.4;
document.write( `${a3}`+ "<br>");
document.write( `${'7.6'}`+ "<br>");
document.write( `${6.7}`+ "<br>");
</script>
</body>
</html>
Output
The output shows numbers in the string format.
Use toLocaleString Method
The toLocaleString method is utilized for transforming a number into its string representation. Its functionality closely resembles that of the toString method in JavaScript.
Syntax
The syntax outlined below demonstrates how to transform a number into a string using the toLocaleString method.
Number.toLocaleString();
Example
The subsequent example demonstrates the utilization of the toLocaleString method to transform numerical values into string representations.
<!DOCTYPE html>
<html>
<head>
<title> How to convert numbers into string using javascript </title>
</head>
<body style = "background-color: beige;">
<h2> How to convert numbers into string using javascript </h2>
<h4> Use toLocaleString() Method to convert number into string </h4>
<script>
let a = 34;
document.write( a.toLocaleString()+ "<br>");
document.write( (45).toLocaleString()+ "<br>");
document.write( (63).toLocaleString()+ "<br>");
document.write( (17).toLocaleString()+ "<br>");
let a1 = 20.02;
document.write( a1.toLocaleString()+ "<br>");
document.write( (6.30).toLocaleString()+ "<br>");
document.write( (5.2).toLocaleString(2.1)+ "<br>");
</script>
</body>
</html>
Output
The output shows numbers in the string format.
Use Lodash _.toString Method
In JavaScript, the Lodash .toString function is utilized to transform a provided character value or numeric value into a string format exclusively. The Lodash .toString function can be employed to change the input value into a string representation. When the input is null or undefined, the function will yield an empty string. Notably, the value of -0 preserves its sign. This method accepts a single parameter to be converted into a string.
Syntax
The syntax provided here is utilized to transform a number into a string by employing the Lodash method _.toString.
_.toString(value);
Example
The subsequent example demonstrates the use of the Lodash .toString method for transforming numbers into string format. Store the provided code in a file named teststring.js.
var _ = require('lodash');
console.log(_.toString(-0));
console.log(_.toString([1, 2, 3]));
console.log(_.toString(['1.1', '1.2', '1.3']));
Command:
Execute the program by running the command provided below.
\>node test_string.js
Output
The output shows numbers in the string format.
Conclusion
JavaScript provides a variety of functions and methods that facilitate the conversion of numbers into strings effortlessly. Depending on the needs of the user and the specifications of the application, we have the flexibility to choose from different methods.