Instead of checking the entire string to see if there's only whitespace, just check to see if there's at least one character of non whitespace:

if (/\S/.test(myString)) {
    // string is not empty and not just whitespace
}
Answer from nickf on Stack Overflow
๐ŸŒ
CoreUI
coreui.io โ€บ answers โ€บ how-to-check-if-a-string-is-empty-in-javascript
How to check if a string is empty in JavaScript ยท CoreUI
September 24, 2025 - This is the same approach we use in CoreUI components for form validation, conditional rendering, and data processing across our component ecosystem. To check for empty or whitespace-only strings, use text.trim().length === 0.
๐ŸŒ
John Kavanagh
johnkavanagh.co.uk โ€บ home โ€บ articles โ€บ check if a string contains only whitespace with js
Check If a String Contains Only Whitespace with JavaScript
July 17, 2023 - Determining whether an input in your application consists of only whitespace needn't be a particularly difficult or complex task. Trim() or Regex will work!
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ check-if-string-is-empty-or-null-javascript
How to Check if a String is Empty or Null in JavaScript โ€“ JS Tutorial
November 7, 2024 - Use strict equality (===) when checking for an empty string. This ensures you don't compare an empty string to a string containing only whitespace characters. Use the trim method to remove leading and trailing whitespace characters before checking ...
๐ŸŒ
Robiul
robiul.dev โ€บ how-to-check-if-a-string-is-empty-in-javascript
How to Check if a String is Empty in JavaScript - Roblog
June 4, 2023 - This allows us to accurately handle strings with whitespace characters. It's important to note that checking for an empty string using the negation operator or combining it with trim() will not handle null and undefined values. If you also need to handle null and undefined, you can modify the condition as follows: javascriptCopy codefunction checkIfEmpty(str) { if (!str || str.trim() === '') { console.log('String is empty'); } else { console.log('String is NOT empty'); } }
๐ŸŒ
Shaikhu
shaikhu.com โ€บ how-to-check-if-a-string-is-null-blank-empty-or-undefined-using-javascript
How to check if a string is null, blank, empty or undefined using JavaScript? - shaikhu.com
The above code works for both ... white spaces. ... Here we are using JavaScript's trim() function to remove all the white spaces from both ends of the string and then check if its empty....
๐ŸŒ
Wikitechy
wikitechy.com โ€บ tutorials โ€บ javascript โ€บ check-for-an-empty-string-in-javascript
[Solved-100% Working Code]- Check for an empty string in javascript - javascript Tutorial - Wikitechy
Returns true for string "0" and whitespace " ". ... If we want to ensure that the string is not just a group of empty spaces (In our imagining this is for form validation) we need to do a replace on the spaces. ... javascript check if string is empty or whitespacejavascript is empty arraycheck if string is empty javajavascript is empty objectjquery empty stringjavascript create empty stringlodash empty stringjavascript isemptyjavascript check if string is empty or nulljquery check if string is empty or whitespacejquery check string empty or whitespacehow to check blank space in javascriptjquer
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ javascript-check-empty-string-checking-null-or-empty-in-js
JavaScript Check Empty String โ€“ Checking Null or Empty in JS
November 7, 2024 - As with the previous method, if we have white spaces, this will not read the string as empty. So we must first use the trim() method to remove all forms of whitespace:
Find elsewhere
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-check-if-string-contains-only-spaces
Check if a String contains Spaces in JavaScript | bobbyhadz
Check if String contains any Whitespace in JavaScript ยท The String.trim() method removes the whitespace characters from both ends of a string. ... We check if the result of calling the method on the string returns a string of length 0. ... ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-program-to-check-if-a-string-contains-any-whitespace-characters
JavaScript - Check If a String Contains any Whitespace Characters - GeeksforGeeks
July 23, 2025 - Use filter() to extract all whitespace characters from the string. If the filtered array is not empty, the string contains whitespace.
๐ŸŒ
GitHub
gist.github.com โ€บ EdCharbeneau โ€บ 9552248
Javascript test a string to see if it is Null or Whitespace. Equivelent to C# String.IsNullOrWhiteSpace ยท GitHub
String.prototype.isNullOrWhiteSpace = function() { return (!this || this.length === 0 || /^\s*$/.test(this)) } ... if i have @Html.EditorFor(m=>m. name) @Html.EditorFor(m=>m. lastname) how i defind that name is empty
๐ŸŒ
Coding Beauty
codingbeautydev.com โ€บ home โ€บ posts โ€บ how to check if a string is empty in javascript
How to Check if a String is Empty in JavaScript - Coding Beauty
July 18, 2022 - To check if a string is empty in JavaScript, we can compare the string with an empty string ('') in an if statement. ... function checkIfEmpty(str) { if (str === '') { console.log('String is empty'); } else { console.log('String is NOT empty'); ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i check for an empty/undefined/null string in javascript?
How do I Check for an Empty/Undefined/Null String in JavaScript? | Sentry
This logical && operator expression returns true if a string is empty or consists of white space and line terminator strings only. YoutubeHow Sentry.io saved me from disaster (opens in a new tab) ResourcesImprove Web Browser Performance - Find the JavaScript code causing slowdowns (opens in a new tab)
๐ŸŒ
Medium
medium.com โ€บ @python-javascript-php-html-css โ€บ how-to-check-for-empty-undefined-or-null-strings-in-javascript-d8f0bf514ead
How to Use JavaScript to Check for Null, Empty, or Undefined Strings
August 24, 2024 - You can check for an empty string using value === โ€œโ€. What is the difference between null and undefined in JavaScript? null represents the intentional absence of a value, whereas undefined indicates a variable has been declared but not assigned ...
๐ŸŒ
MSR
rajamsr.com โ€บ home โ€บ javascript string empty: how to avoid the dreaded error
JavaScript String Empty: How To Avoid The Dreaded Error | MSR - Web Dev Simplified
March 8, 2024 - Handling empty strings in JavaScript is a fundamental aspect of writing clean and reliable code. By using the methods weโ€™ve discussed, such as checkinglength, applying trim(), or utilizing regular expressions, you can effectively detect and handle empty, null, undefined, and whitespace strings.
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ javascript-check-if-string-is-empty
How to check if a string is empty in JavaScript
October 23, 2022 - You can use the length property to check if a string is empty in JavaScript. If the string's length is equal to 0, then it is empty. Otherwise, the string is not empty. const str = '' if (str.length === 0) { console.log(`String is empty โœ…`) ...
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ javascript check if string is empty or whitespace
JavaScript check if string is empty or whitespace
April 26, 2023 - In this example, the regular expression /^\s*$/ matches any string that starts with zero or more whitespace characters (\s*) and ends immediately ($). The test() method returns true if the regular expression matches the string, and false otherwise. if the string is null or undefined, you should check for that separately before using the regular expression, like this: const str = null; // or undefined const isEmptyOrWhitespace = str === null || str === undefined || /^\s*$/.test(str); if (isEmptyOrWhitespace) { console.log("String is empty or contains only whitespace"); } else { console.log("String is not empty and contains non-whitespace characters"); }
๐ŸŒ
Squash
squash.io โ€บ how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
Using the trim() method is particularly useful when dealing with user input, where leading or trailing whitespace might be present. It ensures that whitespace-only strings are considered empty, providing more accurate validation.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript-program-to-check-if-a-string-contains-any-whitespace-characters
JavaScript โ€“ Check If a String Contains any Whitespace Characters | GeeksforGeeks
November 26, 2024 - Use filter() to extract all whitespace characters from the string. If the filtered array is not empty, the string contains whitespace.