loading...
Cover image for The JavaScript String Method - Split();

The JavaScript String Method - Split();

celebritydev profile image Saviour Essien ・2 min read

Hello Fam 😍, 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 😎. Happy New Year πŸ’₯ πŸ’₯.

This post was made as part of my "Did You Know?" series on Twitter. 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.

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.

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.

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.

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

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

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

Use Case

split method use case

Thank you for taking the time to read through my post. I hope you find it useful πŸ’–. I'd appreciate contributions or questions.

I am currently running this series "Did You Know?" for the 366 Days of 2020 on Twitter, do well to follow me @celebritydev so we can learn together.

Side Note

I was listening to Davido's Sweet in The Middle while writing this post. Go forth and split some strings in the middle πŸ˜‡

Discussion

markdown guide