To javascript check if string contains space (the normal U+0020 character), use includes(" ") or indexOf(" ") >= 0. For any whitespace—tabs, newlines, many Unicode separators—use /\s/.test(str) when find space in string should mean “any whitespace class,” not only a normal space. For javascript check if string is empty or whitespace at the ends, str.trim() === "" is the usual pattern. Related: JavaScript string contains (substring checks) and check if string is number (after trimming input).
Tested on: Node.js v20.18.2. A short note after each snippet describes what you should see in the console.
Quick reference
Literal space checks use " " exactly; any ECMAScript whitespace uses /\s/; trim only answers “nothing meaningful at the edges.”
| Goal | Typical check |
|---|---|
Literal space " " |
includes(" ") or indexOf(" ") >= 0 |
| Tab / newline / Unicode whitespace | /\s/.test(str) |
| Nothing left after trimming ends | str.trim() === "" |
indexOf — substring position (also works for a literal space)
String.prototype.indexOf returns the first index or -1. A second argument starts the search after a given index (useful for multiple occurrences, not only spaces in javascript).
const sentence =
"The water levels across the earth, and that's due to an increase in rain centimetres and worsening climate 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}`);You should see two lines: Index of the first water is 4, then Index of the second water is 153.
Literal ASCII space only
For javascript check if string contains space as the single ASCII space character, compare against " " with indexOf or includes; other separators (tabs, NBSP) will not match.
const sentence = "Well, it is good";
const sentence1 = "Welcome";
function hasAsciiSpace(str) {
return str.indexOf(" ") >= 0;
}
console.log(hasAsciiSpace(sentence));
console.log(hasAsciiSpace(sentence1));You should see two lines: true, then false.
Equivalent with includes:
console.log("a b".includes(" "));You should see one line: true.
Any whitespace — RegExp \s
For other methods for checking whitespace in javascript at the character level, /\s/ matches the ECMAScript whitespace class. Do not use a bare /s/ (that matches the letter “s”). The g flag is not required for RegExp.prototype.test.
const sentence = " Well, it is good";
const sentence1 = "Welcome";
function hasWhitespace(str) {
return /\s/.test(str);
}
console.log(hasWhitespace(sentence));
console.log(hasWhitespace(sentence1));You should see two lines: true, then false.
Leading space in the first string matches the tab-or-space class; Welcome has none.
NBSP vs literal space (Unicode pitfall)
A narrow no-break space (\u00A0) looks like a gap but is not the same code unit as " ":
const s = "a\u00A0b";
console.log(/\s/.test(s), s.includes(" "));You should see one line: true false.
Empty or whitespace-only — trim
For js check if string is empty or whitespace at the start and end only, trim strips those characters; internal spaces still leave a non-empty string after trim.
const isEmptyOrWhitespace = (str) => str.trim() === "";
console.log(isEmptyOrWhitespace(" "));
console.log(isEmptyOrWhitespace(" ok "));You should see two lines: true, then false.
Summary
Literal space vs Unicode whitespace vs ends-only emptiness are three different questions—pick the check that matches your product rule, and watch NBSP when copy-pasted text “looks” spaced.
- Use
includes(" ")orindexOf(" ") >= 0for javascript check if string contains space as literalU+0020only. - Use
/\s/.test(str)when tabs, line breaks, or other ECMAScript whitespace must count toward find space in string. - Use
str.trim() === ""for javascript check if string is empty or whitespace at the ends; combine with/\s/if you must detect internal-only whitespace. - Remember NBSP and similar code units: visible gaps are not always
" ".
References
MDN pages for indexOf, includes, trim, and regular expressions—core references for spaces in javascript and whitespace checks.
- MDN:
String.prototype.indexOf() - MDN:
String.prototype.includes() - MDN:
String.prototype.trim() - MDN: Regular expressions
