JavaScript toString() Method Usage with Examples

Written By - Olorunfemi Akinlua

Getting started with toString() Javascript

A string is a sequence of characters and can be very a powerful data structure to carry out a lot of operations in JavaScript. Though, a primitive data structure, its part in a lot of JavaScript applications is very paramount.

There are different data that are more reasonably stored as strings but are currently in another data form, and therefore, there needs to be a means to convert the data from such type to string, and that’s where toString comes to play in JavaScript.

In this article, we will discuss how to use toString in JavaScript with the Number, String, Object data type.


JavaScript toString in Action

The toString method returns a string representation of an object. Remember, all datatypes (from strings to Boolean) in JavaScript are objects. We can use toString on any datatype to return a String datatype (which can be checked using the typeof operator).


Using toString on Numbers

Numbers as a datatype hold numeric values, and with the toString method can be converted to String values.

Let’s illustrate this by creating num binding with the 15 value, and use the toString method on it and check if its datatype using the typeof operator.

let num = 15;
let strNum = num.toString();
console.log(typeof strNum);



You can see that the value stays the same, but is now of the String type. With numeric values, we can pass the base (or radix) number as an argument to determine how the number is parsed to a string. The default way the numbers are parsed is base-10.

let num = 15;
let strNum = num.toString(4);
console.log(typeof strNum);



In the example above, we passed the value 4 to dictate the number 15 be converted to base 4 which resulted in the number 33 (which is the 15 in base-4).


Using toString on Strings

We can apply the toString method to strings, but it doesn’t change the original string, but can be used to convert a string object into a string as will be seen in the last section.

let str = "15";
let strStr = str.toString();
console.log(typeof strStr);




Using toString on Booleans

We can use the toString on Boolean values (true or false), and get a string type.

let bool = true;
let strBool = bool.toString();
console.log(typeof strBool);




Using toString with Objects

Objects are quite different and would require their own implementation to see some useful representation. When we use the toString method, it would return [object Object] value, where the Object is indicative of the Object type. Let’s illustrate this quickly with a simple Object.

let obj = {
    name: "Olorunfemi",
    age: 25,
    isActive: true,



[object Object]

Now, to show toString in action with Object, we can create a User object, define the toString method, create the user - theUser - object based on the User object, and apply the toString on theUser object.

function User(name, age, isActive) { = name;
    this.age = age;
    this.isActive = isActive;

User.prototype.toString = function () {
    return `${} is ${this.age}, and is ${this.isActive.toString()}`;

let theUser = new User("Olorunfemi", 25, true);



Olorunfemi is 25, and is true

Therefore, we can create our own implementation of the toString method with objects as other datatypes are done natively (e.g., [Number](<>), [BigInt](<>), etc.)



We can apply toString to all datatypes because inherently there are all objects, but all of them have their own implementation which has been covered in the article. For more intrinsic detail, you can check the references



Object.prototype.toString() - JavaScript | MDN (
typeof - JavaScript | MDN (


Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment