Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
split()
string

String split() method in JavaScript

Educative Answers Team

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.


Example

string = "A-quick-brown-fox" //string to be split
array_of_strings = string.split("-") //The split() function 

console.log(string) //note that the split function doesn't change the original string
console.log(array_of_strings)
In the code above, we're splitting the string across the dash ("-") character
svg viewer
Note that the breaking character is itself lost in the process

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 split
array_of_strings = string.split(" ") //The split() function 

console.log(string) //note that the split function doesn't change the original string
console.log(array_of_strings)
Using space character will divide the entire string into individual words

RELATED TAGS

javascript
split()
string
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring