Find out how to use the tostring() method effectively and efficiently in your JavaScript code.
In this article, you’ll discover and learn all about the JavaScript tostring() method, which is used to convert data to strings.
This article will help you master the art of converting data to strings from the basics to advanced usage using .tostring in JavaScript.
What is tostring in JavaScript?
The JavaScript tostring is used to return a string representing the specified object.
It is used internally by JavaScript when an object needs to be displayed as text or when an object needs to be used as a string.
Syntax
toString()
Parameters
The toString() method doesn’t require any parameters by default.
The tostring method is meant to be overridden by derived objects for custom type conversion logic.
How to override the toString() method
Here’s an example of overriding the toString() method:
function Person(name) {
this.name = name;
}
const person1 = new Person("Itsourcecode");
Person.prototype.toString = function personToString() {✅
return `${this.name}`;
};
console.log(person1.toString());
Output:
Itsourcecode
Here’s another example of overriding the toString() method in a custom class:
class Data {
constructor(name, age) {
this.name = name;
this.age = age;
}
✅ toString() {
return `Data: ${this.name} ${this.age}`;
}
}
const data = new Data("Itsourcecode", 18);
console.log(data.toString());
Output:
Data: Itsourcecode 18
In this example, we create a Data class with a name and an age property. Then, we override the toString() method to return a string representation of the object that includes the name and age in the given data.
When we create an instance of the Data class and call its toString() method, it returns the custom string representation that we defined.
What does .toString do in JavaScript?
The .toString() method is used to convert a value to its corresponding string representation. It can be called on various data types, such as numbers, arrays, objects, and more.
When used on primitive data types, such as numbers or booleans, the toString() method is implicitly invoked, and the conversion happens automatically.
Consequently, for complex data types like arrays and objects, developers have the flexibility to customize the conversion process by overriding the toString() method.
How to call toString in JavaScript?
You can call the toString() method on a value by using dot notation.
Here’s an example:
let num = 100;
let str = num.toString();✅
console.log(str);
Output:
100
In this example, we have a variable num with the value 100. Then, we call the toString() method on num using dot notation (num.toString()) and store the result in the variable str.
When we log the value of str to the console, we see that it is now a string with the value “100.”
Here’s an example with an array:
You can also call the toString() method on other data types, such as arrays and objects.
let arr = [1, 2, 3, 4, 5];
let str = arr.toString();✅
console.log(str);
In this example, we have an array arr with the values [1, 2, 3, 4, 5]. Then, we call the toString() method on arr using dot notation (arr.toString()) and store the result in the variable str.
When we log the value of str to the console, we see that it is now a string with the following value:
Output:
"1,2,3,4,5"
Solutions on how to use object.tostring() method in JavaScript?
Here are some examples of using the toString() method on different types of objects:
Solution 1: Use toString() on an array
Here’s an example:
const subjects = ["Math", "English", "Science", "History"];
let text = subjects.toString();✅
console.log(subjects)
Output:
[ 'Math', 'English', 'Science', 'History' ]
Solution 2: Use toString() on an object
Here’s an example:
const person = {
firstName: "It",
lastName: "Sourcecode",
age: 18,
Address: "USA"
};
const keys = person.toString();✅
console.log(person);
Output:
{ firstName: 'It', lastName: 'Sourcecode', age: 18, Address: 'USA' }
Solution 3: Use Object.toString() on an object
Here’s an example:
const person = {
firstName: "It",
lastName: "Sourcecode",
age: 18,
Address: "USA"
};
const keys = Object.toString(person);✅
console.log(person);
Output:
{ firstName: 'It', lastName: 'Sourcecode', age: 18, Address: 'USA' }
Here’s another example of overriding toString for custom objects using Using Object.prototype.toString() on an object:
const person = {
firstName: "It",
lastName: "Sourcecode",
age: 18,
Address: "USA"
};
const keys2 = Object.prototype.toString.call(person);✅
console.log(keys2);
Output:
[object Object]
What is the JavaScript Number toString() Method
The JavaScript number toString() method is used to convert a given number into a string. It will return a string that represents the specified Number object.
Syntax
num.toString(base)
How to use .toString to convert number into a String?
When applying the tostring method on a number, it converts the number to a string representation.
For example of convert a number to a string:
let num = 18;
let str = num.toString();✅
console.log(str);
Output:
"18"
Here’s another example of using base 2 (binary) when converting a number to a string:
let num = 18;
let str = num.toString(2)✅
console.log(str);
Output:
10010
What’s the best way to convert a number to a string in JavaScript?
Here are the different ways to convert a number to a string in JavaScript. The following are common methods which you can easily use:
1. Using the toString() method
The toString() method takes an integer or floating point number and converts it into a string type.
For example:
let samplenum = 18;
let str = samplenum.toString();✅
console.log(str);
Output:
"15"
2. Using the String() function
The String() function creates a primitive string type for the number passed to it.
For example:
let samplenum = 19;
let str = String(samplenum);✅
console.log(str);
Output:
"19"
3. Using template literals
Template literals can put a number inside a string, which is a valid way of parsing an integer or float data type.
For example:
let num = 20;
let str = `${num}`;✅
console.log(str);
Output:
20
4. Concatenating with an empty string
This method is considered one of the fastest ways of converting a number into a string.
For example:
let samplenum = 21;
let str = '' + samplenum;✅
console.log(str);
21
Conclusion
In conclusion, this article provides a detailed discussion on understanding the JavaScript .toString() method, which is used to convert data to strings.
We explain the syntax and usage of the method, including how to override it for custom type conversion.
The article also demonstrates how to call .toString() on various data types. Moreover, solutions are also provided on how to use .toString() on different types of objects and explore the JavaScript Number.toString() method for converting numbers to strings.
This article has multiple approaches to effectively and efficiently use the .toString() method in your JavaScript code.
We are hoping that this article provides you with enough information that helps you understand JavaScript tostring.
You can also check out the following article:
Thank you for reading itsourcecoders 😊.