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.

🌐
W3Schools
w3schools.com β€Ί howto β€Ί howto_js_validation_empty_input.asp
How To Add Validation For Empty Input Field with JavaScript
Learn how to add form validation for empty input fields with JavaScript. <form name="myForm" action="/action_page.php" onsubmit="return validateForm()" method="post" required> Name: <input type="text" name="fname"> <input type="submit" value="Submit"> </form> If an input field (fname) is empty, this function alerts a message, and returns false, to prevent the form from being submitted:
Discussions

What is the best method to check if a variable is not null or empty?
It depends what you mean by empty, or how strict you want to be. These values will coerce to false: undefined null '' (empty string) 0 NaN Everything else coerces to true. So, if you are OK with rejecting all of those values, you can do: if(PostCodeInformation) { } If you want to make sure that PostCodeInformation is really an object value (and not a number or boolean, etc): if(typeof PostCodeInformation === 'object' && PostCodeInformation !== null) { } You have to do the null-check there, because in JavaScript typeof null returns 'object'. So dumb. If you want to make sure that PostCodeInformation has some property that you really need: if(PostCodeInformation && PostCodeInformation.myCoolProperty) { } Etc, etc More on reddit.com
🌐 r/javascript
18
3
August 2, 2015
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
🌐
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.

🌐
Roadmap
roadmap.sh β€Ί questions β€Ί javascript-coding
Top 80 JavaScript Coding Interview Questions and Answers
The event loop is a crucial part of the JavaScript runtime architecture, it helps to handle asynchronous operations. The event loop constantly checks if the call stack is empty and pushes tasks from the task queue onto it.
🌐
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 - To ensure our application is robust and free from unexpected errors, it's important to handle cases where a string might be undefined or null. Considering JavaScript's type coercion and truthy/falsy evaluation, a more encompassing check can be performed to cover empty, undefined, and null strings in a single condition. let myString; if (!myString) { console.log("The string is empty, undefined, or null"); } else { console.log("The string has content"); }
🌐
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 - let myStr = " "; if (myStr.length === 0) { console.log("This is an empty string!"); }else{ console.log("This is NOT an empty string!"); } ... We can easily fix this error by first removing the white spaces using the trim() method before checking ...
🌐
JSONLint
jsonlint.com
JSONLint - The JSON Validator
JSONLint is a validator and reformatter for JSON, a lightweight data-interchange format. Copy and paste, directly type, or input a URL in the editor above and let JSONLint tidy and validate your messy JSON code. JSON (pronounced as Jason), stands for "JavaScript Object Notation," is a human-readable and compact solution to represent a complex data structure and facilitate data interchange between systems.
Find elsewhere
🌐
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 - To check for empty or whitespace-only strings, use text.trim().length === 0. 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 ...
🌐
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.
🌐
Quora
quora.com β€Ί How-do-you-check-if-an-HTML-element-is-empty-using-JavaScript
How to check if an HTML element is empty using JavaScript - Quora
Answer (1 of 2): First of all, you need to define empty. If you want to check that there is nothing inside it (like in Mohamed Nabeel’s answer), you can write a function like this: [code]function isEmpty(element) { return element.innerHTML === '' } [/code]With the function above the results ar...
🌐
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 - In the first script, we create a function called isStringEmpty that accepts a single parameter, value. This function returns true if the value is either undefined, null, or an empty string (β€œβ€).
🌐
ThatSoftwareDude.com
thatsoftwaredude.com β€Ί content β€Ί 8774 β€Ί what-is-the-best-way-to-check-for-an-empty-string-in-javascript
The Best Way to Check for an Empty String in JavaScript - ThatSoftwareDude.com
August 23, 2024 - Discover the most efficient methods to check for an empty string in JavaScript. Learn best practices to ensure your code handles string validation effectively.
🌐
React
react.dev β€Ί reference β€Ί react-dom β€Ί components β€Ί input
<input> – React
Displayed in a dimmed color when the input value is empty. readOnly: A boolean. If true, the input is not editable by the user. required: A boolean. If true, the value must be provided for the form to submit. size: A number. Similar to setting width, but the unit depends on the control. src: A string. Specifies the image source for a type="image" input. step: A positive number or an 'any' string. Specifies the distance between valid values. ... Checkboxes need checked (or defaultChecked), not value (or defaultValue).
🌐
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 - // function to check string is empty or not function checking(str) { if(str.replace(/\s/g,"") == "") { console.log("Empty String") } else{ console.log("Not Empty String") } } checking(" "); checking("Hello 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 works because if one of the ... of the logical OR (||) operator is evaluated. To check for an empty string, the logical && operator is used....
🌐
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 - Next, we check if str is not null. The null value represents the intentional absence of any object value. In javascript, By checking whether the string is null or empty, we ensure that the variable has a valid value.
🌐
Neovim
neovim.io β€Ί doc β€Ί user β€Ί lsp
Lsp - Neovim docs
(string?) err On timeout, cancel, or error, err is a string describing the failure reason, and result is nil. commands vim.lsp.commands Map of client-defined handlers implementing custom (off-spec) commands which a server may invoke. Each key is a unique command name; each value is a function which is called when an LSP action (code action, code lenses, …) requests it by name. If an LSP response requests a command not defined client-side, Nvim will forward it to the server as workspace/executeCommand.
🌐
Attacomsian
attacomsian.com β€Ί blog β€Ί javascript-check-if-string-is-empty
How to check if a string is empty in JavaScript
October 23, 2022 - const str = '' if (str.length === 0) { console.log(`String is empty βœ…`) } else { console.log(`String is not empty ❌`) } // String is empty βœ… Β· If the string contains leading or trailing whitespace, you should use the trim() method to remove whitespace before checking if it is empty:
🌐
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 - This truthy check requires at least one character in the string to succeed. It evaluates to true if the value is not '' (empty string), undefined, null, false, 0, or NaN values.