toString() method in JavaScript

JavaScript’s toString() method is quite uniques in the sense that it can be applied to JavaScript Number, String, Arrays. When invoked on any of these identifier JavaScript interpreter will take value inside identifier and spit it out.

If in case your not aware of what are JavaScript Number, String, Arrays? Then please go to below mentioned links first to learn about these concepts and then come back here to get an idea about what’s toString() method.

Let’s discuss now How toString() JavaScript method can be applied on these different types of identifiers?

Invoking toString() method on String

Invoking toString() method on a String in JavaScript just spit out value of string.
• If String is primitive data type then toString() method just return its value.
• If String is a string wrapper object then toString() will internally unwrap it to primitive string and then returns its value.

Syntax for invoking toString() method on Stringstring_name.toString()

ReturnsString

Below is an example showing How to invoke toString() method on a string in JavaScript?

var x = "Computer Science Hub";
var y = x.toString();
console.log(y);        // Returns "Computer Science Hub"

Invoking toString() method on Number

Invoking toString() methods on a Number just works like that on a String.
• If Number is primitive data type then toString() method takes its value and return it as a String.
• If Number is a number wrapper object then toString() will internally unwrap it to primitive number, takes its value and return it as a String.

Syntax for invoking toString() method on Numbernumber_name.toString()

ReturnsString

Below is a code example showing How to invoked toString() method on a number in JavaScript?

var a = 19382;
var b = a.toString();
console.log(b);           // Returns "19282" string

Bonus Point 🥳
How to change number to string in JavaScript?
Number can be changed to string in JavaScript by just invoking toString() method on it. For example – 8594.toString() will change number 8594 to “8594” string
.

Invoking toString() method on JavaScript Array

Invoking toString() method on array will just change list of array elements(commas still being there) to a string.

For Example
[ 10, 29, 13, 28 ].toString() will return “10,29,13,29”(a JavaScript String)

For knowing more about this please see – Array methods JavaScript toString.

Conclusion

JavaScript’s toString() method can be quite handy for changing other data types to string data type. This is only one type of conversion in JavaScript rather it’s kind of a feature of it to do type conversions internally meaning whenever JavaScript Engine finds that some specific type is needed to be passed onto a function and type which is passed is not that then it would just do internal magic and programmer wouldn’t even this change. Sometimes this can lead to confusions as well, but mostly this feature of doing internal type conversions by JavaScript is very useful.

Moreover if your learning JavaScript Programming Language, I’ve put together a number of articles covering different aspects of JavaScript. You can check those out here – Computer Science Hub JavaScript.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts