Number to String in JavaScript

In this article, we will explore the process of transforming a number into a string in JavaScript.

There are many methods that we can utilize to convert a number to string in JavaScript which are as follows:

  • Utilizing the toString Method
  • Utilizing the String function
  • Concatenating an Empty String
  • Utilizing the Template Strings
  • Utilizing the toLocaleString Method
  • Utilizing the Lodash _.toString Method

We will explore each technique individually, utilizing demonstrations to enhance our understanding.

Utilizing toString Method

The toString function transforms a number into its string representation.

Syntax:

Example

number.toString();

Demo:

We will employ the toString method to convert a number into a string using the code provided below.

Code:

Example

let a = 15;

console.log(a.toString());
console.log((25).toString());
console.log((30).toString(3));
console.log((10).toString(2));

Output:

The following output illustrates how a number can be converted into a string utilizing the toString function.

Utilizing the String function

The String function serves the purpose of transforming an integer or a floating-point number into a string format.

Syntax:

Example

String(object);

Demo:

We will employ the String function to convert the numeric value into a string format utilizing the provided code.

Code:

Example

let z = 36;
let y = 45;	
let x = 56;

console.log("Converting a number '36' to String =", String(z));
console.log("Converting a number '45' to String =", String(y));
console.log("Converting a number '56' to String =", String(x));

Output:

In the output provided, we can observe that a number has been converted into a string using the String function.

Concatenating an Empty String

The concatenation technique is an straightforward approach for transforming an integer or floating-point number into a string format. This method effectively combines the number with the string.

Syntax:

Example

let variableName =' ' + value;

Demo:

We will employ the concatenation technique to convert the number into a string format.

Code:

Example

let number = 15;
console.log("Converting 10 to String =", (number + '') );

let floatNumber = 63.256;
console.log("Converting 20.12200 to String =", (floatNumber + ''));

Output:

The following output demonstrates the conversion of a number into a string through the use of the concatenation technique.

Utilizing Template Strings

Template strings are employed to embed an integer or a floating-point number within a string.

Syntax:

Example

let var_name = '${value}';

Demo:

We will employ template strings to convert the number into a string format.

Code:

Example

let number = 24;
let f_number = 15.25;
let str = `${number}`;
let floatNumberString = `${f_number}`;
console.log("Converting 24 to a string =", str);
console.log("Converting 15.25 to a string =", floatNumberString);

Output:

In this output, we can observe how a numerical value is transformed into a string through the use of template literals.

Utilizing toLocaleString Method

The toLocaleString function is one of the available methods for transforming a number into a string representation.

Demo:

We will employ the toLocaleString function to convert the number into a string format.

Syntax:

Example

num.toLocaleString();

Code:

Example

let num = 105;
let numString = num.toLocaleString();
console.log("Converting number '105' to a string =", numString);

Output:

Below is the result demonstrating the conversion of a number into a string format through the use of the toLocaleString function.

Utilizing Lodash _.toString Method

The Lodash method _.toString serves the purpose of transforming a numerical value into a string representation.

Demo:

We will employ the Lodash _.toString function to convert the number into a string format.

Code:

Example

const _ = require("lodash");

console.log(_.toString(405));

Output:

In this output, we can distinctly observe how a numeric value is transformed into a string by employing the Lodash _.toString method.

Conclusion:

We have comprehended how to convert a number to string in JavaScript in this article. Following are some of the methods that are utilized to change a number to a string:

  • toString Method
  • String function
  • Concatenating an Empty String
  • Template Strings
  • toLocaleString Method
  • Lodash _.toString Method

Input Required

This code uses input(). Please provide values below: