JavaScript String.toString() Method

Shubham Vora Jan 30, 2023
  1. Syntax of JavaScript toString():
  2. Example Codes: Convert Number to String With Base 2, 8, & 16
  3. Example Codes: Convert JavaScript Array to String Using the toString() Method
  4. Example Codes: Convert a JavaScript Function to a String Using the toString() Method
JavaScript String.toString() Method

The toString() method converts any variable or object to a string. There is a toString() method on each JavaScript object.

When an object has been utilized as a string or presented as text, JavaScript uses the toString() function internally. A string wrapper also can be returned as a string via the toString() method.

The original string remains unaltered when we use toString().

Syntax of JavaScript toString():

number.toString(base);
object.toString();

Parameters

base It is a base for the number string. It must fall between 2 and 36 as an integer.
number It is a number to convert to the string based on the base value.
object It can be the array or objects to convert to the string.

Return

The number as a string or the object as a string.

Example Codes: Convert Number to String With Base 2, 8, & 16

Calling the toString() method with the parameters 2, 8, and 16 allows us to convert numbers to a string with bases 2, 8, and 16.

In the example, the n1 is the number passed to the toString() method, which converts it to a string of base 2 and stores it in the result1 variable. Similarly, we are converting n2 to its related octal number string.

When we apply the toString() method on the n3 with base 16, it returns the hexadecimal number related to n3.

var n1 = 177;
var result1 = n1.toString(2);
console.log("Base 2: "+result1);
var n2 = 100;
var result2 = n2.toString(8);
console.log("Base 8: " + result2);
var n3 = 202;
var result3 = n3.toString(16);
console.log("Base 16: " + result3);

Output:

Base 2: 10110001
Base 8: 144
Base 16: ca

Example Codes: Convert JavaScript Array to String Using the toString() Method

The array is the collection of different types of variables in JavaScript. Users can call the toString() method by taking the array as a reference to convert it into the text format.

In the example below, arr takes a string array input, and when the user applies the toString() method, the array elements get converted to comma-separated string literals.

var arr = ["Apple","Banana","Strawberry","Pineapple","Orange"];
var str1 = arr.toString();
console.log(str1);

Output:

Apple,Banana,Strawberry,Pineapple,Orange

Example Codes: Convert a JavaScript Function to a String Using the toString() Method

In the example below, we have created the JavaScript function named test and used the toString() method to convert the function to a normal text format.

var func = function test(){
    let temp = 20;
    return temp;
}
var funcStr = func.toString();
console.log(funcStr);

Output:

function test(){
    let temp = 20;
    return temp;
}

Most recent browsers support the toString() method. To convert a number, array function, or a string object to a string, users can use the toString() built-in function of JavaScript.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub

Related Article - JavaScript String