JS Arrays( join() metod )

Hello. This time I have a rather simple question, rather a clarification.
Arrays > Converting between strings and arrays:

let myNewString = myArray.join(',');
myNewString;

Array.prototype.join():

const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join());
// expected output: "Fire,Air,Water"

Check:

const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join(','));
Fire,Air,Water

Do I understand correctly that there is no need for a comma in .join () if you want to separate the array with only commas?
which is better?

Yes, that is correct. The comma is the default separator. So if you ommit it, commas will automatically be used.
If you look at join() and it’s clear for you what it means, ommit it. If you have to think about what it means everytime, it may be better to explicitly write join(',').
In the end, you have to decide for yourself, if you want to write it or not.

I hope that helps.

1 Like