DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,911 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for The JavaScript String Method - Split();
Saviour Essien
Saviour Essien

Posted on

The JavaScript String Method - Split();

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 πŸ˜‡

Top comments (3)

Collapse
 
giftimade1 profile image
Gift Imade

Wow this is lovely like Naija Music

Collapse
 
celebritydev profile image
Saviour Essien Author

Thank you Gift.

Collapse
 
iamoluwase profile image
Olu

Download latest Nigerian Songs at Madnaija.com

This post blew up on DEV in 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!