The JavaScript String Method - Split();
Tags: javascript, didyouknow, tutorial, celebritydev
Published: 2020-01-03 21:11:19

Hello Fam :heart_eyes:, I have been on dev.to for two months now. I read the articles most of the time and I love it here. This is my first post here :sunglasses:. Happy New Year :boom: :boom:.

This post was made as part of my "Did You Know?" series on [Twitter](https://twitter.com/celebritydev). I wanted to explain more about the string split method concept. In this post, I will explain what split(); does and a use case of how to apply it.

# Explanation

The split is a JavaScript **String** method for modifying and manipulating strings. When using the split() method it returns an **array of substrings** of the string value. Take a look at the code snippets below.

```javascript
const hobbies = "Travelling, Reading, Coding, Cuddling";
let hobby = hobbies.split(',');
console.log(hobby); Result // ["Travelling", " Reading", " Coding", " Cuddling"]
```

Let me explain the snippet, the split() method can take in a **separator** which in this case is a comma (",") it separated the string after each comma sign (,). The split method can also take a **limit** (optional) and one other form of the separator of which we will discuss shortly. Notice that the resulting array has a spacing before each word? We would get rid of that extra spacing by doing this.


```javascript
const hobbies = "Travelling, Reading, Coding, Cuddling";
let hobby = hobbies.split(', ');
console.log(hobby); Result // ["Travelling", "Reading", "Coding", "Cuddling"]
```

I have added an extra space after the comma (, ) separator to fix the unwanted space.

You can also use the **limit** to end where you want the split should end the array length.

```javascript
const hobbies = "Travelling, Reading, Coding, Cuddling";
let hobby = hobbies.split(', ', 2);
console.log(hobby); Result // ["Travelling", "Reading"]
```

The separator can also separate the string using space.

```javascript
const hobbies = "Travelling Reading Coding Cuddling";
let hobby = hobbies.split(' ');
console.log(hobby); Result // ["Travelling", "Reading", "Coding", "Cuddling"]
```

Notice I have gotten rid of the comma(,) in the **hobbies variable**, then I am separating using space as a separator. This will create a new array value after each space.

Without the space in between the split(""), the string will split between each character like so

```javascript
const move = "Legwork";
let dance = hobbies.split('');
console.log(dance); Result // ["L","e","g","w","o","r","k"]
```

An empty split() will have no effect on the original string instead it will return a single array value

```javascript
const move = "Legwork";
let dance = hobbies.split();
console.log(dance); Result // ["Legwork"]
```



# Use Case

![split method use case](https://res.cloudinary.com/theboringdude/image/upload/v1578083786/splitjs.jpg "String split method use case")


Thank you for taking the time to read through my post. I hope you find it useful :sparkling_heart:. I'd appreciate contributions or questions.

I am currently running this series "Did You Know?" for the 366 Days of 2020 on [Twitter](https://twitter.com/celebritydev), do well to follow me [@celebritydev](https://twitter.com/celebritydev) so we can learn together.

### Side Note
I was listening to Davido's [**Sweet in The Middle**](https://www.naijaloaded.com.ng/download-music/davido-ft-wurld-naira-marley-zlatan-sweet-in-the-middle) while writing this post. Go forth and split some strings in the middle :innocent: