array to string javascript – JavaScript Array toString() Method and using join() here Method – array to string javascript, I will learn how to convert JavaScript array to string with or without commas. Free Demo with Example code included.
JavaScript array to string (with and without commas)
//array to string javascript let numbers = [9, 8, 7, 6]; let numbersToString = numbers.toString(); console.log(numbersToString); // output is "9,8,7,6"
use the JSON.stringify method:
let members = [ { name: "pakainfo" }, { name: "infinityknow" }, { name: "w3diy" }, { name: "w3school" }, ]; let membersToString = JSON.stringify(members); console.log(membersToString); // [{"name":"pakainfo"},{"name":"infinityknow"},{"name":"w3diy"},{"name":"w3school"}]"
JavaScript array to string without commas
join accepts one string parameter
[9, 8, 7].join(); // "9,8,7" [9, 8, 7].join("+"); // "9+8+7" [9, 8, 7].join(" "); // "9 8 7"
Also You can even pass an empty user string to the method:
[9, 8, 7].join(""); // 987
don’t Miss : Javascript Concatenate String And Variable
Converting an array of numbers
const numArray = [6, 7, 4]; numArray.toString(); // expected Result: 6,7,4
Converting an array of strings
const strArray= ['p', 'a', 'k']; strArray.toString(); // expected Result: p,a,k
Converting mix arrays (both numbers and strings)
const productInfo = ['7', 98, 'Tamilrokers']; productInfo.toString(); // expected Result: 7,98,Tamilrokers
Converting a String back to an Array
const str = productInfo.toString(); const resultData = str.split(','); console.log('resultData: ', resultData); // expected Result: ["7", "98", "Tamilrokers"]
Working with Nested Arrays
const arrInArr = [ '7', 98, [ 'Tamilrokers', 4 ] ]; arrInArr.toString(); // expected Result: 7,98,Tamilrokers,4
Example – array to string javascript
const objInArr = ['7', 98, { name: 'infinityknow', age: 58 } ]; objInArr.toString() ; // expected Result: 7,98,[object Object]
I hope you get an idea about array to string javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.