Last updated: Mar 3, 2024
Reading timeยท4 min
To check if a string contains only spaces:
String.trim()
method to remove the leading and trailing whitespace
from the string.0
.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
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.
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
.
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.
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.
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.
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
We used the RegExp.test() method to check if a string contains only spaces.
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.
\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.
String.match()
Alternatively, you can use the String.match()
method.
The expression will return true
if the string contains only spaces and false
otherwise.
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
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.
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.
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
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.
\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
.
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'); }
String.includes()
If you only want to check if the string contains spaces, use the
String.includes()
method.
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
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
.
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.
function containsWhitespace(str) { return /\s/.test(str); } console.log(containsWhitespace('hello\tworld')); // ๐๏ธ true console.log(containsWhitespace('hello\nworld')); // ๐๏ธ true
String.match
You can also use the String.match()
method to check if a string contains
whitespace.
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 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.
You can learn more about the related topics by checking out the following tutorials: