The syntax of this method is very simple: array.toString().
The toString() method therefore returns a string.
Since the original array remains unchanged, so we store the result in a variable and then use the string obtained where needed.
Where the base represents the system in which to translate.
We can also omit the base in order to simply get the decimal number in a string.
var number = 20; console.log("String: " + number.toString());
But let’s make an example by specifying the base. Let’s convert a decimal number to binary:
var number = 20; console.log("Binary : " + number.toString(2));
Convert an array of numbers to a string using the toString() method.
So suppose we have the following array of numbers:
var numbers = [5,4,3,2,6,9,62];
We then use the toString method to convert the array and store the result in a variable such as a string name.
Finally we display the result in the browser console.
var stringNumbers = numbers.toString(); console.log(stringNumbers);
The same could be done with an array of strings, so let’s repeat the example above by changing only the array.
For example, if we therefore have the following array:
var objectsSchools = ['pencil', 'eraser', 'sharpener', 'notebook', 'pencil'];
Then we apply the toString method to the array of starting strings and view the result for simplicity in the console.log.
var str = objectsSchools.toString(); console.log(str);
As in the previous example, once again we see a string in the console of our browser.