Empty string, undefined, null, ...

To check for a truthy value:

if (strValue) {
    // strValue was non-empty string, true, 42, Infinity, [], ...
}

To check for a falsy value:

if (!strValue) {
    // strValue was empty string, false, 0, null, undefined, ...
}

Empty string (only!)

To check for exactly an empty string, compare for strict equality against "" using the === operator:

if (strValue === "") {
    // strValue was empty string
}

To check for not an empty string strictly, use the !== operator:

if (strValue !== "") {
    // strValue was not an empty string
}
Answer from Brian Dukes on Stack Overflow
Top answer
1 of 16
5116

Empty string, undefined, null, ...

To check for a truthy value:

if (strValue) {
    // strValue was non-empty string, true, 42, Infinity, [], ...
}

To check for a falsy value:

if (!strValue) {
    // strValue was empty string, false, 0, null, undefined, ...
}

Empty string (only!)

To check for exactly an empty string, compare for strict equality against "" using the === operator:

if (strValue === "") {
    // strValue was empty string
}

To check for not an empty string strictly, use the !== operator:

if (strValue !== "") {
    // strValue was not an empty string
}
2 of 16
1448

For checking if a variable is falsey or if it has length attribute equal to zero (which for a string, means it is empty), I use:

function isEmpty(str) {
    return (!str || str.length === 0 );
}

(Note that strings aren't the only variables with a length attribute, arrays have them as well, for example.)

Alternativaly, you can use the (not so) newly optional chaining and arrow functions to simplify:

const isEmpty = (str) => (!str?.length);

It will check the length, returning undefined in case of a nullish value, without throwing an error. In the case of an empty value, zero is falsy and the result is still valid.

For checking if a variable is falsey or if the string only contains whitespace or is empty, I use:

function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}

If you want, you can monkey-patch the String prototype like this:

String.prototype.isEmpty = function() {
    // This doesn't work the same way as the isEmpty function used 
    // in the first example, it will return true for strings containing only whitespace
    return (this.length === 0 || !this.trim());
};
console.log("example".isEmpty());

Note that monkey-patching built-in types are controversial, as it can break code that depends on the existing structure of built-in types, for whatever reason.

🌐
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 - In this first method, we will check for the length of the string by adding the length property. We'll check if the length is equal to 0. If it’s equal to zero, it means that the string is empty, as we can see below:
Top answer
1 of 1
2
It adds an empty string because there is no code telling it not to. Computers do the exact thing your code tells it to and nothing beyond that. Since the code for adding an item simply instructs JavaScript to take the text value from the field and add it, that's what it does. Whether the text input is empty or not does not matter at all. As for how to avoid that you just have to explicitly write code to check if the input is empty and stop the function if it is. The simplest way of coding that would probably be something like this: JavaScript addItemButton.addEventListener('click', () => { if (addItemInput.value.trim() === "") { // check if the (trimmed) input textbox is an empty string return; // Return (stop the function) if that is the case } let ul = document.getElementsByTagName('ul')[0]; let li = document.createElement('li'); li.textContent = addItemInput.value; ul.appendChild(li); addItemInput.value = ''; }); There are two things in the above code you might not be familiar with. The first is trim(), that is simply a method that removes white space (spaces, tabs, etc) from the start and end of a string. Without that you could enter a space into the text input box and that would be considered a non empty string and would therefore be added. The second is that I use the return keyword without specifying any value, this is because of the fact that I'm not using return in the way you are probably used to seeing it used. I'm not using it to pass some data from the function back to some other place, I'm simply making use of the fact that when a function returns it stops running, which means that all of the code below the return statement is not executed.
🌐
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 - In this example, we're first using the trim method to remove any leading or trailing whitespace characters from the str variable, then checking whether the resulting string has zero length.
🌐
Position Is Everything
positioniseverything.net › home › javascript empty string: how to check and when to use them
JavaScript Empty String: How To Check and When To Use Them - Position Is Everything
November 11, 2025 - In the example above, the str is a variable that returns false for an empty, null, undefined option. It will only return true if not empty string or whitespace is found. Let us provide a detailed example to explain this process. ... You can also use replace to check for an empty string JavaScript option.
🌐
W3docs
w3docs.com › javascript
How to Check for Empty/Undefined/Null String in JavaScript
let emptyStr; if (!emptyStr) { // String is empty } If the string is empty/null/undefined if condition can return false: ... let undefinedStr; if (!undefinedStr) { console.log("String is undefined"); } let emptyStr = ""; if (!emptyStr) { ...
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-check-for-an-empty-string-in-javascript.php
How to Check for an Empty String in JavaScript
Topic: JavaScript / jQueryPrev|Next · You can use the strict equality operator (===) to check whether a string is empty or not. The comparsion str === "" will only return true if the data type of the value is string and it is not empty, otherwise return false as demonstrated in the following example: Try this code » ·
🌐
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 - For a more comprehensive check including null and undefined, use !text || text.length === 0. The strict comparison === 0 is preferred over == 0 to avoid type coercion issues and ensure precise empty string detection. ... Follow Łukasz Holeczek on GitHub Connect with Łukasz Holeczek on LinkedIn Follow Łukasz Holeczek on X (Twitter) Łukasz Holeczek, Founder of CoreUI, is a seasoned Fullstack Developer and entrepreneur with over 25 years of experience. As the lead developer for all JavaScript, React.js, and Vue.js products at CoreUI, they specialize in creating open-source solutions that empower developers to build better and more accessible user interfaces.
🌐
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 - Document your code to clarify how empty strings are handled. Regularly test your code with various input scenarios to ensure robustness. To append to an empty string, you can simply use the += operator. Here is an example:
Find elsewhere
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript empty string | example code
JavaScript empty string | Example code
July 15, 2022 - To define JavaScript empty a string use single quote or double quote without text. var s; // undefined var s = ""; // "" or ' ' s.length // 0 · Test whether strValue is empty or is None · if (strValue) { //do something } Test wheter strValue ...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript check if string is empty
How to Check for an Empty String in JavaScript | Delft Stack
March 11, 2025 - In this example, the trim() method is applied to str, which consists solely of spaces. After trimming, its length is checked. If the length is zero, the string is considered empty or whitespace. This method is particularly useful when dealing with user inputs, as it ensures that only meaningful content is evaluated. Another elegant way to check for an empty string is by using the logical NOT operator (!). In JavaScript...
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-empty-undefined-null-strings-in-javascript
How to check empty/undefined/null strings in JavaScript?
In JavaScript, "" represents the empty string, and we can use the null keyword to initialize the string with a null value. If we don't assign any value to any variable, it is undefined by default. Sometimes, we need to check if the string is empty, undefined, or null while working with the strings.
🌐
Roblog
robiul.dev › how-to-check-if-a-string-is-empty-in-javascript
How to Check if a String is Empty in JavaScript
June 4, 2023 - Javascript treats empty strings as falsy values, which means they evaluate to false in a boolean context. We can take advantage of this behavior to check if a string is empty using the negation operator (!). ... In this example, the isEmptyString function takes a string as an argument and returns true if the string is empty, and false otherwise.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-check-if-string-is-empty
How to check if a String is Empty in JavaScript | bobbyhadz
Copied!const str = 'bobbyhadz.com'; if (typeof str !== undefined && str !== null && str !== '') { // 👇️ this runs console.log( 'The value is NOT undefined, null or empty string', ); } else { console.log('The value is undefined, null or empty string'); } We used the logical AND (&&) operator, so for the if block to run, all conditions have to be met. ... The variable doesn't store an undefined value. The variable doesn't store a null value. The variable is not an empty string.
🌐
TutorialKart
tutorialkart.com › javascript › javascript-create-empty-string
How to create an Empty String in JavaScript? - TutorialKart
November 18, 2022 - To create an empty string in JavaScript, do not provide any value in double quotes, single quotes, or back-ticks. Or, we can also use String() constructor and pass no parameter in the constructor call. The following are some of the quick examples to create an empty string in JavaScript.
🌐
Quora
quora.com › What-does-an-empty-string-do-in-JavaScript
What does an empty string do in JavaScript? - Quora
Answer (1 of 4): Well, it’s either a String object or a string primitive, so it does all the same things any String or string does. As a String object, it knows all the String methods, even if some of them are pretty trivial when the value is an empty string.
Top answer
1 of 1
4
Hi Tabish Kazi, Without looking at the video, I can tell you that in the specific example given, the second code example would lead to the same result as the first code example. The real difference lies in the operator used. In the first example you use the plus-equal operator which reads the current value of the variable and adds the value to the right of the operator to it, then stores the new value to the variable. In the second example, the value to the right of the assignment operator is stored in the variable with no preference to what is already stored in the variable. The thing is though, in order for the plus-equal operator to work, the value of the variable can’t be null or undefined. So say you were looping through a series of properties and you wanted to dynamically create an HTML list item for each valid property without knowing how many list items would need to be created in advance. Well you know you’re going to wrap all this HTML within a string (and later set the innerHtml of some element to be equal to this string). And so you want to continually build upon this HTML string for however long the for loop runs. And so you decide to use the plus-equal operator to add to this string, but if the variable you’re adding to isn’t already a string, then the JS interpreter wouldn’t be able to add the first list item to it (a string of html defined declaratively). The program/code would crash. This is because the interpreter wouldn’t know how to add an undefined value and a string together (just as the interpreter wouldn’t know how to add something like a string and an integer together). So you initialize the variable as an empty string so that the interpreter knows that the variable is indeed a string, and as such, can be concatenated with other strings. I’m thinking that maybe I’ve over explained a bit, but I hope that makes sense. Let me know if it doesn’t.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-empty-undefined-null-string-in-javascript
How to Check empty/undefined/null String in JavaScript? - GeeksforGeeks
July 11, 2025 - This approach uses length property to get the length of string and ! operator. and by using ! operator we will check string is empty or not.
🌐
Futurestud.io
futurestud.io › tutorials › check-if-a-string-is-empty-in-javascript-or-node-js
Check if a String is Empty in JavaScript or Node.js
I’m the maintainer of the @supercharge/strings package providing convenient string utilities. The @supercharge/strings package comes with a handy Str#isEmpty method. This isEmpty method determines whether the wrapped value is an empty string.