As programmers we always face challenges and deal with different value types, frequently we need to parse a number into its string form for different purposes.
Regarding this matter and as everything in software development there are many ways to achieve this. Today we are going to dig into one of them, specifically Number.toString() method offers a convenient way to deal with.
Let's start quoting what the documentation has to say about this method.
The Number object overrides the toString() method of the Object object. (It does not inherit Object.prototype.toString()). For Number objects, the toString() method returns a string representation of the object in the specified radix.
What it states is the method is not a simply form of a string, it actually overrides the default method and not also converts a number to string but does that on an a specific radix.
Here we have an example of the simplest form about the Number.toString() work.
let numberToBeString = 123 console.log(numberToBeString.toString())
and the result, as expected is 123 but as string value. Nothing we already do not know :)
Now let's make make something more interesting, what about if we need the representation of a number in its binary form. Guess what, Number.toString() comes to the rescue.
let numberToBeString = 123 console.log(numberToBeString.toString(2))
As you can see the method accepts a parameter as the radix, so we can now guess what the result will be. And if you say 1111011 you're right! that's because as we specify the radix the method parses the number into a string but also converts it to the given radix.
Now that we approach further to one way of transforming numbers based on different radix and if you are still eager to get more knowledge about it here you can find the official documentation
Have a great coding day!