There are many ways to split a string by multiple spaces in JavaScript. In this article, I will introduce two of them using built-in JavaScript methods like indexOf(), split(), … etc. Want to know how I will do it, then follow this article. I will give a few examples so you understand it and can do it again.
How did I split a string by multiple spaces in JavaScript?
In this article, I will use two ways to split a string by multiple spaces in JavaScript:
- Split a string by multiple spaces using the splice() and indexOf() method
- Split a string by multiple spaces using the trim() method
Both of these methods use the split() method to split a string by multiple spaces, so we should understand the above method as its syntax and usage.
Syntax
string.split(separator, limit)
Parameters
- separator: Is the marker to separate the string
- limit: is the maximum number of elements of the returned array
The split() method will return an array containing the values separated from the array with ‘separator’ as the indicator to split the string.
After learning about the split() method, we will go into detail about how I can do it to split a string by multiple spaces.
Use splice() and indexOf() methods
Example
const str = " apple lemon orange "; const result = str.split(" "); console.log(result);
Output
['', '', 'apple', '', '', '', 'lemon', '', '', '', 'orange', '', '']
After splitting the string split the string with the split() method, we see that there are redundant elements, so I will use the splice() and indexOf() methods to remove those elements so that we get the correct result.
splice() method
Syntax
splice(start, deleteCount)
Parameters
- start: Is the position you want to change
- deleteCount: is the number of elements you want to delete
indexOf() method
Syntax
indexOf(searchElement)
Parameter
- searchElement: is the value of the element you want to find
After learning all the methods involved, we will split a string by multiple spaces as follows:
const str = " apple lemon orange "; const arrStr = str.split(" "); while (arrStr.indexOf("") != -1) { arrStr.splice(arrStr.indexOf(""), 1); } console.log(arrStr);
Output:
['apple', 'lemon', 'orange']
I used the while loop to check if empty elements in the array separated from the split() method are still there, or if still, we will use the splice() method to remove them from the ‘result’ array after parts are gone If the element is empty, the program will output the same result as we expect.
Use the trim() method
In this way, I will use the trim() method to remove extra spaces at both ends of the string then I will use the split() method to combine regular expressions to get the most accurate result.
Syntax
string.trim()
Parameter
- NONE
I will use the trim() method and split() method to split a string by multiple spaces as follows:
const str = " apple lemon orange "; const arrStr = str.trim().split(/\s+/); console.log(arrStr);
Output
['apple', 'lemon', 'orange']
We see this way we get the same result as when we use splice() and indexOf() methods in combination with the split() method, but our program will be compact, and the code will be more optimized.
Summary
Above are the two ways I show you to split a string by multiple spaces in JavaScript. When we use the trim() method, our program will be more compact, and execution time will be faster than when we use the splice() and indexOf() methods in combination with the split() method. Try both ways to understand how to choose one for yourself. I hope this article helps you and your program. Good luck.

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css