Back to Home
Strings

How to Split and Join Strings

Use .split() to convert string to array, .join() to convert array to string

Quick Answer (2024 ES6+ Way)

javascript
const text = 'hello-world-javascript';
const words = text.split('-');
console.log(words); // ['hello', 'world', 'javascript']
const rejoined = words.join(' ');
console.log(rejoined); // "hello world javascript"

Live Example

javascript
const csv = 'John,Doe,30,Engineer';
const fields = csv.split(',');
console.log(fields);

// Transform and rejoin
const formatted = fields.map(f => f.trim()).join(' | ');
console.log(formatted);

Common Variations

Split by Multiple Characters
javascript
const text = 'a,b;c:d';
const parts = text.split(/[,;:]/);
// ['a', 'b', 'c', 'd']
Limit Split
javascript
const text = 'a-b-c-d';
const parts = text.split('-', 2);
// ['a', 'b']

❌ Don't Do This (Outdated Way)

Avoid manual string parsing with loops

javascript
// DON'T DO THIS
var result = '';
for (var i = 0; i < arr.length; i++) {
  result += arr[i];
  if (i < arr.length - 1) result += ',';
}

Browser Support

Works in all modern browsers (ES3+)

#split#join#strings