Converting an Array to a String in JavaScript

Joined
Jun 29, 2022
Messages
28
Reaction score
0
I have an array of strings in JavaScript, and I need to convert it into a single string with specific delimiter characters between the elements. Here's a sample array:

JavaScript:
let myArray = ["apple", "banana", "cherry", "date"];

I want to convert this array into a single string with a comma and space (", ") between each element. The desired output should look like this:

Code:
"apple, banana, cherry, date"

Please send me the JavaScript code I need to do this conversion. If I wish to use a different character or characters between the array components, how can I change the delimiter? I looked for a solution by visiting many sources, but I was unsuccessful. Thank you.
 
Joined
Aug 22, 2023
Messages
42
Reaction score
7
It seems you would like this to happen as the result of conducting an if statement! Although, can I inquire as to what you would like to assign the variables.

If you would like to conduct an if statement, the result of the statement should prompt a change in textContent, innerText, or innerHTML, depending on user input. This is a common DOM algorithm.

JavaScript:
<script>
if, else, addEventListener
varone.textContent="Lorem ipsum...";
</script>
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
use the toString command.

Code:
let myArray = ["apple", "banana", "cherry", "date"];
let text = myArray.toString();

and the array will not be changed.
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Use just join(char/chars)
JavaScript:
const myArray = [ 'apple', 'banana', 'cherry', 'date' ];
const myString = myArray.join(', '); // in this case: comma and space
console.log(myString, myArray)


BTW, check this ;)
JavaScript:
const myArray = [ 'apple', 'banana', 'cherry', 'date' ];

console.info('1', myArray.toString());
console.info('2', myArray.join());
console.info('3', myArray.join(','));
console.info('4', myArray.join(''));
 
Last edited:
Joined
Jul 3, 2022
Messages
93
Reaction score
23
JavaScript:
const myArray = [ 'apple', 'banana', 'cherry', 'date' ];
const sum = myArray.reduce((sum, current) => sum + ',' + current);
console.log( sum ); // apple,banana,cherry,date

🤣
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
You can also just use a loop ;)
JavaScript:
const myArray = [ 'apple', 'banana', 'cherry', 'date' ];
let myString = '';
for (const item of myArray)
  myString += item + ', ';

myString = myString.slice(0, -2);
console.log(myString);
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
Another loop variant ☢️ ( absolutely useless but still working )

JavaScript:
const myArray = [ 'apple', 'banana', 'cherry', 'date' ];
let sum = '';

while( myArray.length ){
    sum += myArray.shift() + ( myArray.length ? ', ' : '' );
}

console.log( sum ); // apple, banana, cherry, date
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top