One way to convert a JavaScript array into a string is to concatenate an empty string after it.
For instance, we can write:
const arr = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday']
const str = arr + ""
console.log(str)
Then str
is:
'Sunday,Monday,Tuesday,Wednesday,Thursday'
Use the Array.prototype.toString Method
Another way to convert a JavaScript array into a string is to use the JavaScript array toString
method.
For example, we can write:
const arr = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday']
const str = arr.toString();
console.log(str)
Then we get the same result as before.
Use the Array.prototype.join Method
Another way to convert a JavaScript array into a string is to use the JavaScript array join
method.
We pass in a delimiter to join the strings in the array with and it’ll return a stirring with all the string array entries joined together.
For example, we can write:
const arr = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday']
const str = arr.join(", ");
console.log(str)
And str
is:
'Sunday, Monday, Tuesday, Wednesday, Thursday'