Check if a String contains Spaces in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
4 min

banner

# Table of Contents

  1. Check if a String contains only Spaces in JavaScript
  2. Check if String contains any Whitespace in JavaScript

# Check if a String contains only Spaces in JavaScript

To check if a string contains only spaces:

  1. Use the String.trim() method to remove the leading and trailing whitespace from the string.
  2. Check if the length of the string is 0.
  3. If the condition is met, the string contains only spaces.
index.js
function containsOnlySpaces(str) { return str.trim().length === 0; } console.log(containsOnlySpaces(' ')); // ๐Ÿ‘‰๏ธ true console.log(containsOnlySpaces(' 123 ')); // ๐Ÿ‘‰๏ธ false console.log(containsOnlySpaces('bobbyhadz.com ')); // ๐Ÿ‘‰๏ธ false console.log(containsOnlySpaces('')); // ๐Ÿ‘‰๏ธ true

check if string contains only spaces

The code for this article is available on GitHub

If you need to check if a string contains any whitespace, click on the following subheading.

The String.trim() method removes the whitespace characters from both ends of a string.

index.js
console.dir(' bobby '.trim()); // ๐Ÿ‘‰๏ธ 'bobby' console.dir(' '.trim()); // ๐Ÿ‘‰๏ธ ''

We check if the result of calling the method on the string returns a string of length 0.

index.js
console.dir(' bobby '.trim().length); // ๐Ÿ‘‰๏ธ 5 console.dir(' '.trim().length); // ๐Ÿ‘‰๏ธ 0

If it does, we either have a string that contains only spaces or an empty string.

The String.trim() method doesn't change the original string, it returns a new string. Strings are immutable in JavaScript.

An alternative, but also very common approach is to use a regular expression.

# Check if a String contains only spaces using RegExp.test()

You can also use the RegExp.test() method.

The test() method will return true if the string contains only spaces and false otherwise.

index.js
function containsOnlySpaces(str) { return /^\s*$/.test(str); } console.log(containsOnlySpaces(' ')); // ๐Ÿ‘‰๏ธ true console.log(containsOnlySpaces(' 123 ')); // ๐Ÿ‘‰๏ธ false console.log(containsOnlySpaces('bobbyhadz.com ')); // ๐Ÿ‘‰๏ธ false console.log(containsOnlySpaces('')); // ๐Ÿ‘‰๏ธ true

check if string contains only spaces using regexp test

The code for this article is available on GitHub

We used the RegExp.test() method to check if a string contains only spaces.

The method returns true if the regular expression is matched in the string and false otherwise.

The forward slashes / / mark the beginning and end of the regular expression.

The caret ^ matches the beginning of the input and the dollar sign $ matches the end of the input.

The \s character matches a single space, tab or newline.

The asterisk * character matches the preceding item (space) 0 or more times.

If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

# Check if a String contains only spaces using String.match()

Alternatively, you can use the String.match() method.

The expression will return true if the string contains only spaces and false otherwise.

index.js
function containsOnlySpaces(str) { return str.match(/^\s*$/) !== null; } console.log(containsOnlySpaces(' ')); // ๐Ÿ‘‰๏ธ true console.log(containsOnlySpaces(' 123 ')); // ๐Ÿ‘‰๏ธ false console.log(containsOnlySpaces('bobbyhadz.com ')); // ๐Ÿ‘‰๏ธ false console.log(containsOnlySpaces('')); // ๐Ÿ‘‰๏ธ true

check if string contains only spaces using string match

The code for this article is available on GitHub

The String.match() method returns the result of matching a string against a regular expression.

The method returns an array containing the matches (if any) or null if no matches are found.

We compared the output of the method to null to get a boolean result.

Which approach you pick is a matter of personal preference. I'd go with the trim() method as I find it easier to read and more beginner-friendly.

# Check if String contains Whitespace in JavaScript

Use the test() method to check if a string contains whitespace.

The test method will return true if the string contains at least one whitespace character and false otherwise.

index.js
function containsWhitespace(str) { return /\s/.test(str); } console.log(containsWhitespace(' ')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('bobby hadz com')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('')); // ๐Ÿ‘‰๏ธ false console.log(containsWhitespace('test')); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

We used the RegExp.test() method to check if a string contains whitespace.

The test() method returns true if the regular expression is matched in the string and false otherwise.

The forward slashes / / mark the start and end of the regular expression.

The \s character is used to match spaces, tabs and newlines.

Our regular expression checks for all types of whitespace characters, including tabs \t and newline characters \n.

index.js
function containsWhitespace(str) { return /\s/.test(str); } console.log(containsWhitespace('hello\tworld')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('hello\nworld')); // ๐Ÿ‘‰๏ธ true if (containsWhitespace('hello\nworld')) { // ๐Ÿ‘‡๏ธ this runs console.log('The string contains whitespace'); } else { console.log('The string does NOT contain whitespace'); }

# Check if String contains Whitespace using String.includes()

If you only want to check if the string contains spaces, use the String.includes() method.

index.js
function containsWhitespace(str) { return str.includes(' '); } console.log(containsWhitespace(' ')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('bobby hadz com')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('')); // ๐Ÿ‘‰๏ธ false console.log(containsWhitespace('test')); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

Note that the includes() method doesn't check for all types of whitespace, it checks only for spaces.

The String.includes() method returns true if the supplied substring is contained in the string and false otherwise.

The includes() method would return false if your string contains only tabs \t or newline characters \n.

index.js
function containsWhitespace(str) { return str.includes(' '); } console.log(containsWhitespace('bobby hadz')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('hello\tworld')); // ๐Ÿ‘‰๏ธ false console.log(containsWhitespace('hello\nworld')); // ๐Ÿ‘‰๏ธ false

If you also want to test for tabs and newline characters, use the regex approach.

index.js
function containsWhitespace(str) { return /\s/.test(str); } console.log(containsWhitespace('hello\tworld')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('hello\nworld')); // ๐Ÿ‘‰๏ธ true

# Check if a string contains whitespace using String.match

You can also use the String.match() method to check if a string contains whitespace.

index.js
function containsWhitespace(str) { return str.match(/\s/) !== null; } console.log(containsWhitespace('bobby hadz')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('hello\tworld')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('hello\nworld')); // ๐Ÿ‘‰๏ธ true console.log(containsWhitespace('bobbyhadz.com')); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

The String.match() method returns the result of matching a string against a regular expression.

The method returns an array containing the matches (if any) or null if no matches are found.

We compared the output of the method to null to get a boolean result.

Which approach you pick is a matter of personal preference. I'd go with the RegExp.test() method because I find it more direct and intuitive.

If you ever need help reading a regular expression, check out this regular expression cheat sheet by MDN.

It contains a table with the name and the meaning of each special character with examples.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev