The split()
method in JavaScript enables us to break down a string into an array of (smaller) strings. Let’s jump directly into the code.
string = "A-quick-brown-fox" //string to be splitarray_of_strings = string.split("-") //The split() functionconsole.log(string) //note that the split function doesn't change the original stringconsole.log(array_of_strings)
The break character can be any available character. The codes below show the exact same task using space (" “) and empty (”") characters.
string = "A quick brown fox" //string to be splitarray_of_strings = string.split(" ") //The split() functionconsole.log(string) //note that the split function doesn't change the original stringconsole.log(array_of_strings)
Free Resources