Check if string contains spaces in JS [SOLVED]

Written by - Olorunfemi Akinlua
Reviewed by - Deepak Prasad


Operations with strings vary from splitting to the removal of whitespaces to checking if a string contains spaces. JavaScript provides built-in string methods and features that allow us to manipulate or work with strings. Strings are immutable, and so the way we work with them can be a little difficult compared to arrays (even though both are sequences).

In this article, we will discuss and explain to check if the string contains spaces using two approaches - the indexOf method and regular expression.


Different methods to check if string contains spaces in JavaScript

Method-1: Use indexOf method

Not to be confused with the array indexOf method, the String.prototype.indexOf() takes a string argument and searches for it within the string it’s called upon and returns the index of the first occurrence of the string argument.

Let’s work with the indexOf method, and search for the word water inside the sentence binding, and log the starting index of the word inside the sentence.

const sentence =
    "The water levels across the earth, and that's due to an increase in rain centimetres and worsening climae change and maybe because due to ice melting to water too (who knows)";
const search = "water";
const indexOfSearchWord = sentence.indexOf(search);
const SecondIndexOfSearchWord = sentence.indexOf(search, indexOfSearchWord + 1);

console.log(`Index of the first ${search} is ${indexOfSearchWord}`);
console.log(`Index of the second ${search} is ${SecondIndexOfSearchWord}`);


Index of the first water is 4
Index of the second water is 153

You can also see that we can find the second occurrence by passing a second argument (a number) to the indexOf method which specifies the indexOf method to check for the string argument after an index greater than or equal to the specified number.

So with the understanding of the indexOf method, we can make use of it to check if the string contains spaces by passing a whitespace string to the method. Since the method returns an index when a whitespace exists, we can check if the value is greater than or equal to zero which returns true if there is a whitespace in any index of the string and false if otherwise.

const sentence = "Well, it is good";
const sentence1 = "Welcome";

function hasSpace(sentence) {
    return sentence.indexOf(" ") >= 0;





Method-2: Use Regular Expressions

Regular expressions are patterns that we can make use of to match string character combinations, and in addition, we can make use of built-in methods such as exec and test. The test method returns a Boolean value that indicates whether or not a pattern exists in a searched string, and we will use the appropriate string character combination to check if the string contains spaces (or whitespaces are present within the string).

The pattern for whitespace is /s/, and the /g represents a global search/match, and we will use it together with the test method to check for whitespace across the string, and if present, it returns true.

const sentence = " Well, it is good";
const sentence1 = "Welcome";

function hasSpace(sentence) {
    return /\\s/g.test(sentence);






The string indexOf method and regular expression method are great and excellent approaches to check if string contains spaces, and a good understanding of how strings work and regular expressions are needed.



String.prototype.indexOf() - JavaScript | MDN (
Regular expressions - JavaScript | MDN (


Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on LinkedIn.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment