JavaScript split string | Based on a delimiter, comma, space

JavaScript split() method is used to split a string. This function is split a given string into an array of substrings and returns the new array.

It will use a separator and if no separator used then string (“”) is used as the separator. So the result is the string is split between each character.

Note: JS split() method doesn’t change the given string.


Parameter Values

  • separator– Specifies the character to use for splitting the string, (Optional)
  • limit–  items after the split limit will not be included.

Return value

It returns an Array, containing the splitted values.

Examples of JavaScript split string

Let’ see the multiple example of js split string based on delimiter:-

1. Split string by space

Split string into an array of substrings using space as a separate in split method.

You can do same this for split string by comma in JavaScript.

Example with space, try comma( or other delimiter or char) by yourself.


Split string by space example

2. Separate each character

If you will not pass any separator then it will use empty string (“”) is used as the separator and split between each character including spaces.


split each character, including whitespace

3. Use the limit parameter in split() method

Setting a limit to 3.

JavaScript split string

Example JavaScript split regex

You can use regular expression for splitting, lets split date string in js.


Example JavaScript split regex

Q: How to Split string into two parts in JS?

Answer: Use indexof() method for it.

How to javascript split string at index?

Answer: Use JS slice() method, to slipt a string by index.

Output and Read more Examples:- Slice method in JavaScript | Used Slice the String and Array

Do comment if you have any doubts and suggestion on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.