# Join() and split() in javascript

**URL:** <https://forum.codewithmosh.com/t/join-and-split-in-javascript/17538>\
**Category:** JavaScript\
**Created:** [January 8, 2023, 1:30pm UTC](https://forum.codewithmosh.com/t/join-and-split-in-javascript/17538 "2023-01-08T13:30:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![YuriAmancio](https://avatars.discourse-cdn.com/v4/letter/y/e47c2d/32.png) [@YuriAmancio](https://forum.codewithmosh.com/u/YuriAmancio)\
**Post date:** [January 8, 2023, 1:30pm UTC](https://forum.codewithmosh.com/t/join-and-split-in-javascript/17538/1 "2023-01-08T13:30:55Z")

</div>

In Javascript course in the Array section, Mosh gives an example that takes a string and transform it in an array using the split() method, and then he takes this array and transform it back in a string using the join() method:

```auto
const message = 'This is my first message';
const parts = message.split(' ');
console.log(parts);

//output: (5) ['This', 'is', 'my', 'first', 'message']

const combined = parts.join('-');
console.log(combined);

//output: This-is-my-first-message

```

why would we need to take a string, transform it into an array, and then back to a string?

---

<div class="post-metadata">

**Author:** ![thanidasi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/thanidasi/32/5339_2.png) [@thanidasi](https://forum.codewithmosh.com/u/thanidasi)\
**Post date:** [January 8, 2023, 1:58pm UTC](https://forum.codewithmosh.com/t/join-and-split-in-javascript/17538/2 "2023-01-08T13:58:40Z")

</div>

Well, in the example you’ve provided, the string `'This-is-my-first-message'` that has been created by joining parts is different from the initial string `'This is my first message'` as in the newer string, spaces have been replaced by dashes.  
There can be other similar use cases where you might want to replace a certain character with some other character in a string. Of course, you can achieve the same thing as done in the example using `replace` string method like `'This-is-my-first-message'.replace(/ /g, '-')` but it’s just another (and simpler) way of doing it.

---

<div class="post-metadata">

**Author:** ![AzharUddinSheikh](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/azharuddinsheikh/32/5561_2.png) [@AzharUddinSheikh](https://forum.codewithmosh.com/u/AzharUddinSheikh)\
**Post date:** [January 9, 2023, 3:41pm UTC](https://forum.codewithmosh.com/t/join-and-split-in-javascript/17538/3 "2023-01-09T15:41:56Z")

</div>

> [@YuriAmancio](#):
>
> why would we need to take a string, transform it into an array, and then back to a string?

Have you ever wonder how you can create a `slug`  
look into the `link` of this question **`https://forum.codewithmosh.com/t/join-and-split-in-javascript/17538`**

when you submitted this question you have wrote something like this `Join() and split() in javascript` it ended up `join-and-split-in-javascript`
