.split()
In JavaScript, the .split()
method returns a new array of substrings based on a given string. This method is useful for parsing CSV data, processing user input, or breaking apart text for analysis.
Syntax
string.split(separator, limit);
Parameters:
separator
(Optional): Describes the pattern where each split should occur.limit
(Optional): Determines the number of substring elements included in the returned array.
Return value:
The .split()
method returns a new array of substrings based on a given string.
Note: If
separator
is not provided, the returned array will contain the entirestring
as its lone element.
Example 1: Using .split()
Without limit
This example uses the .split()
method without the limit
parameter to split the stringOfNames
string into an array of names:
const stringOfNames = 'Dominic, Shelly, Luka, Devin';console.log('No limit:', stringOfNames.split(', '));
Here is the output:
No limit: [ 'Dominic', 'Shelly', 'Luka', 'Devin' ]
Example 2: Using .split()
with limit
This example uses the .split()
method with the limit
parameter to split the stringOfNames
string into an array of names limited to 3 elements:
const stringOfNames = 'Dominic, Shelly, Luka, Devin';console.log('Limited to 3 elements:', stringOfNames.split(', ', 3));
Here is the output:
Limited to 3 elements: [ 'Dominic', 'Shelly', 'Luka' ]
Codebyte Example: Using .split()
Without Parameters
This example uses the .split()
method without any parameters to split the stringOfNames
string into an array of names in a string:
Frequently Asked Questions
1. Can I use regular expressions with .split()
?
Yes. Regular expressions can be used with .split()
for more flexible and complex splitting logic:
// Uses the separators '-' and '_'let substr = 'one-two_three'.split(/[-_]/);console.log(substr); // [ 'one', 'two', 'three' ]
2. What if the separator in .split()
is an empty string?
Splitting on an empty string using .split()
breaks the string into an array of individual characters:
let substr = 'ABC'.split('');console.log(substr); // [ 'A', 'B', 'C' ]
3. What happens with consecutive separators in .split()
?
Empty strings appear in the result if there are consecutive separators in .split()
:
let substr = 'a,,b'.split(',');console.log(substr); // [ 'a', '', 'b' ]
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours