JavaScript

How to Use toString() Method in JavaScript

JavaScript makes it possible to convert one type of data into another without manually modifying its values. For example, you have to write a program for performing a number to string conversion. This specified operation can be performed implicitly when the equality operator “==” is used or if the added value’s data type is incompatible.

However, JavaScript also offers a built-in method primarily utilized for explicitly converting a data type into a string. The JavaScript toString() method is used to represent an array or a number as a string while converting an object to a string, you have to override the “toString()” method so that it can print out the values of the object’s keys.

This write-up will discuss the procedures to use the toString() method in JavaScript. So, let’s start!

Converting number to string using toString() method

The “toString()” method can be used for number to string conversion. For this purpose, pass the desired mathematical “base” as an argument, and the “toString()” method will convert the specified number according to the defined base.

For example, we will create a variable named “number” having “433” value:

var number = 433;

Then, we will invoke the “to.String()” method while passing “2” as the number base:

console.log("String with base 2 : " + number.toString(2));

The execution of the “toString()” method will return a value “110110001” as the representation of integer “433” in the “binary” number system:

Similarly, you can convert any number to its “octal” representation by passing “8” as the base argument:

console.log("String with base 8 : " + number.toString(8));

The given output signifies that the number “433” is represented as “661” in the octal number system:

The “to.String()” method also permits the conversion of a number to its “hexadecimal” representation. For this purpose, specify “16” as the base argument:

console.log("String with base 16: " + number.toString(16));

As you can see, in the hexadecimal number system, the number “433” equals to “1b1”:

If the “toString()” method is invoked without passing any argument, then the “number” will be converted to “string” without changing the current base:

console.log("String: " + number.toString());

Output

Converting array to string using toString() method

The “toString()” method can be applied to any type of array, and it returns its elements in a string format. For instance, we have created an array named “numberArray” that has three elements: “34”, “23”, and “43”:

const numberArray= [34, 23, 43];

Next, we will convert the “numberArray” to a string by utilizing the “toString()” method:

numberArray.toString();

Note that the values displayed in output are enclosed in quotes ‘ ‘ not in square brackets:

In the same way, the “toString()” method can be invoked for converting an array of strings to a single string which will comprise all of the values enclosed in the quotes ‘ ‘:

const stringArray= ['x', 'y', 'z'];
stringArray.toString();

Output

When “toString()” is used with an “array” that contains another array inside of it, then the “toString()” method first “flatten” it and then returns all values in a string format, separated by a comma.

For instance, the below given “array” has two elements: “Paul” and “32” and a nested array which further comprises two elements. Now, when the “toString()” method is called as “array.toString()”, it will reduce arrays’ dimensionality and return all of the four elements in a single string:

const array =[ 'Paul', 32, [ 'Max', 4 ] ];
array.toString();

Output

Converting object to string using toString() method

With the help of the “toString()” method, you can perform object to string conversion.

For instance, we will create an “object” that has the following key-value pairs:

const object = { name: 'Paul', age: 40 };

After doing so, we will invoke the “toString()” method:

object.toString();

The output of the given program will print out the string “[object, Object]” which indicates that “object” belongs to the “Object” base class:

However, you can override the “toString()” method to return the values of the object keys in a string format.

In the below-given program, the “Employee” object will override the “toString()” method which is inherited from the “Object” base class. This user-defined “toString()” method will return a string containing the values of the “name” and “age” properties of the created “employee” object:

function Employee(name, age) {
this.name= name;
this.age = age;
 }
Employee.prototype.toString = function () {
return 'Employee Name: '+this.name + ' Age: '+ this.age;
 }

employee1 = new Employee('Alex', 35);
console.log(employee1.toString());

Output

That was all of the essential information related to the JavaScript toString() method. You can further research about it according to your requirements.

Conclusion

The JavaScript “toString()” method can be utilized to represent an array or a number as a string. When a number is converted to a string, you must specify the desired “base” as an argument; otherwise, the toString() method only converts the number into a string without changing its base. The toString() method is also utilized for retrieving the values of an object’s keys in a string format. This write-up discussed the use of the JavaScript toString() method.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.