JavaScript toString() Method Usage with Examples


Written By - Olorunfemi Akinlua
Advertisement

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(strNum)
console.log(typeof strNum);

Output

15
string

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.

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

Output

33
string

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(strStr);
console.log(typeof strStr);

Output

15
string

 

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(strBool);
console.log(typeof strBool);

Output

true
string

 

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,
};

console.log(obj.toString());

Output

[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) {
    this.name = name;
    this.age = age;
    this.isActive = isActive;
}

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

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

console.log(theUser.toString());

Output

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](<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString>), [BigInt](<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString>), etc.)

 

Summary

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

 

References

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

 

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