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.

🌐
Just Academy
justacademy.co › blog-detail › how-to-check-empty-string-in-javascript
How To Check Empty String In JavaScript
An empty string will have a length of 0. 2) Using the triple equals operator: You can compare the string directly to an empty string using the triple equals operator (===) to check if it is empty.
Discussions

Javascript empty string is not empty
Maybe its not an empty string, and instead a string of one or more invisible characters. const id = '­' // or '\u00AD' if stripped by reddit console.log(id) // '' console.log(id.trim().length) // 1 More on reddit.com
🌐 r/learnjavascript
12
3
March 2, 2022
Why do we have to use an empty string while declaring the variable 'items' first?
Tabish Kazi is having issues with: On line 11, Why did we have to declare the variable 'items' first and store an empty string value inside it; and then on line 13 add to ... More on teamtreehouse.com
🌐 teamtreehouse.com
1
August 31, 2020
Replace empty string to null in form submit, and JS limitation in Changeset Object
I might be asking trivial questions but I really couldn't figure them out In a very simple pgsql form submit, some of foreign key need to be null instead of "" otherwise I get below error My first thought is to use … More on community.retool.com
🌐 community.retool.com
1
1
December 18, 2023
JavaScript String.split(RegExp) is returning empty strings
So, I believe that what's going on here is that the Javascript you are running is using the split method, while the regex is matching. Split is actually "splitting" the string into parts. So when it matches the dot . in your regex, it splits it into document, ., write, . Your regex, on the other hand matches the dot .. You see that just with the first character, you're already skewed 3 to 1. If you're wanting to replicate the functionality of what you have on regex101, you'd want to use the match instead. Here is a demo More on reddit.com
🌐 r/regex
5
2
July 19, 2017
🌐
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:
🌐
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 - For example, you have a form where a user can input their name. If the user doesn't input anything, the input field's value will be an empty string. However, the value will be null if the input field is not even created. JavaScript has several ways to check whether a string is empty or null.
🌐
Reddit
reddit.com › r/learnjavascript › javascript empty string is not empty
r/learnjavascript on Reddit: Javascript empty string is not empty
March 2, 2022 -

They closed my question on SO because it's not reproducible, but that's exactly why I posted, because code isn't behaving as it should.

Anyway, I 'm receiving a JSON result from a web service. It looks something like this:

{ "data": [{ "id": "123ABC", "name" : "Test 1" }, { "id": "", "name" : "Test 2" }] }

I 'm looping through the data array and need to determine if an id exists or not:

for( const item of data ) {
    if( item.id !== null && item.id.trim().length > 0 ) {
        doSomething();
    } else {
        doSomethingElse();
    }
}

My problem is that doSomething() fires for the first item ("123ABC") but also fires for the second where the id is empty.

I've tried spitting out the values for the second item:

console.log("NULL ", item.id === null);
console.log("EMPTY ", item.id.trim().length === 0);

and results are

NULL  false
EMPTY  false

so I'm wondering if there's something strange about the id value.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › split
String.prototype.split() - JavaScript | MDN
For example, a string containing tab separated values (TSV) could be parsed by passing a tab character as the separator, like myString.split("\t"). If separator contains multiple characters, that entire character sequence must be found in order to split. If separator appears at the beginning (or end) of the string, it still has the effect of splitting, resulting in an empty (i.e., zero length) string appearing at the first (or last) position of the returned array.
Find elsewhere
🌐
JavaScript in Plain English
javascript.plainenglish.io › taming-the-void-mastering-empty-strings-in-javascript-55f335f07195
JavaScript - Taming the Void: Mastering Empty Strings | by Ange Loron | Apr, 2024 | JavaScript in Plain English | Medium
April 4, 2024 - In JavaScript, values can be classified as either “truthy” or “falsy.” Truthy values are those that are considered true when evaluated in a boolean context, while falsy values are those that are considered false. ... let strValue = “Hello, World!”; if (strValue) { console.log(“strValue was a non-empty string, true, 42, Infinity, [], …”); }
🌐
Medium
medium.com › @mhangoyewo › how-an-empty-string-in-javascript-ruined-my-afternoon-29a603cd3d19
How an Empty String in JavaScript Ruined My Afternoon | by Mhango Yewo | Medium
January 21, 2024 - Who would have thought that calling Number with an empty string would actually return 0? By then, I considered myself to be pretty well-versed with the various tricky behaviours of JavaScript's type coersion, but it turns out that that was not quite enough.
🌐
Sam Jarman
samjarman.co.nz › blog › empty-string
Empty String Considered Harmful
July 30, 2024 - The use of empty strings when used to indicate a null value or lack of value , when the language you're using has a better way to support that will, will lead to bugs and hard to maintain code.
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.
🌐
DEV Community
dev.to › richclarke0 › function-to-check-for-an-empty-string-in-one-line-of-javascript-17bi
Function to check for an empty string in one line of Javascript 😎 - DEV Community
August 9, 2022 - Hi there, folks. Here you go: const isEmpty = (string) => !string.trim() Enter... Tagged with javascript, protip, beginners.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
Objects are first converted to a primitive by calling its [Symbol.toPrimitive]() (with "string" as hint), toString(), and valueOf() methods, in that order. The resulting primitive is then converted to a string. There are several ways to achieve nearly the same effect in JavaScript.
🌐
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)
🌐
JavaScript in Plain English
javascript.plainenglish.io › why-returns-an-empty-string-in-javascript-743fd244a624
Why [] + [] Returns an Empty String in JavaScript 🤯 | by Udbhav | JavaScript in Plain English
July 9, 2025 - In JavaScript, arrays are objects. But when the engine needs to convert them to primitives (like strings or numbers), it uses .toString(). ... You’re essentially concatenating two empty strings!
🌐
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.
🌐
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 - Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero.
🌐
W3Schools
w3schools.com › js › js_string_methods.asp
JavaScript String Methods
2 weeks ago - If the separator is omitted, the returned array will contain the whole string in index [0]. If the separator is "", the returned array will be an array of single characters: ... For a complete reference to all JavaScript properties and methods, with full descriptions and many examples, go to:
🌐
Zipy
zipy.ai › blog › how-do-i-check-for-an-empty-undefined-null-string-in-javascript
how do i check for an empty undefined null string in javascript
April 12, 2024 - Understanding these distinctions is crucial for effective string handling and manipulation in JavaScript. An empty string in JavaScript is represented by "" (a string with no characters).
🌐
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
March 23, 2023 - 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.
🌐
Retool
community.retool.com › 💬 app building
Replace empty string to null in form submit, and JS limitation in Changeset Object - 💬 App Building - Retool Forum
December 18, 2023 - I might be asking trivial questions but I really couldn't figure them out In a very simple pgsql form submit, some of foreign key need to be null instead of "" otherwise I get below error My first thought is to use inline js to replace "" with null inside Changeset -> Object -> {{form.data}}, but immediately I get run into a error object.keys() is not a function shown in below screenshot.