While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or
edited it to ensure you have an error-free learning experience. It’s on our list, and we’re working on it!
You can help us out by using the “report an issue” button at the bottom of the tutorial.
Sometimes you just need to take strings apart:
var sentence = "Oh a cookie!"
sentence.split(" ");
// [ "Oh", "a", "cookie!" ]
Syntax
The trick is using the correct separator:
myArray.split(separator);
Common use case
If you leave the separator blank, it will dissect each character.
var pieces = sentence.split("");
// [ "O", "h", " ", "a", " ", "c", "o", "o", "k", "i", "e", "!" ]
Notice that the spaces stay in the resulting array because they were not used as the separator.
Learn More
split()
is just one of several methods that help developers work with strings, to learn more see How To Index, Split, and Manipulate Strings in JavaScript