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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › Empty
Empty statement - JavaScript | MDN
An empty statement is used to provide no statement, although the JavaScript syntax would expect one.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Selection › empty
Selection: empty() method - Web APIs | MDN
const log = document.getElemen... newSelectionHandler(); // The button cancel all selection ranges const button = document.querySelector("button"); button.addEventListener("click", () => { selection.empty(); });...
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.

🌐
SitePoint
sitepoint.com › blog › javascript › test for empty values in javascript
Test for Empty Values in Javascript — SitePoint
November 6, 2024 - For example, if you want to check if a variable x is empty, you can do: if (x == null) { // x is null or undefined } This will return true if x is either null or undefined. In JavaScript, null is an assignment value that represents no value or no object. It is an intentional absence of any ...
🌐
W3Schools
w3schools.com › jquery › html_empty.asp
jQuery empty() Method
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
🌐
jQuery
api.jquery.com › empty
.empty() | jQuery API Documentation
Description: Remove all child nodes of the set of matched elements from the DOM · This method removes not only child (and other descendant) elements, but also any text within the set of matched elements. This is because, according to the DOM specification, any string of text within an element ...
🌐
Sentry
sentry.io › sentry answers › javascript › how do i test for an empty javascript object?
How do I Test for an Empty JavaScript Object? | Sentry
It will be equal to an empty object string if it is: ... This method is slower than the other methods. If you don’t know if the value is an object, you’ll need to add some extra checks to determine whether it is. First, check if the value is null or undefined: ... If you don’t do this check, you’ll get the following error if the value of value is null or undefined: ... You can end up with false positives if the value is a JavaScript object constructor, such as new Date() or new RegExp():
Find elsewhere
🌐
W3Schools
w3schools.com › howto › howto_js_validation_empty_input.asp
How To Add Validation For Empty Input Field with JavaScript
Create a Website Make a Website ... Header Example Website · 2 Column Layout 3 Column Layout 4 Column Layout Expanding Grid List Grid View Mixed Column Layout Column Cards Zig Zag Layout Blog Layout · Google Charts Google Fonts Google Font Pairings Google Set up Analytics · Convert Weight Convert Temperature Convert Length Convert Speed · Get a Developer Job Become a Front-End Dev. Hire Developers ... Learn how to add form validation for empty input fields with JavaScript...
🌐
SamanthaMing
samanthaming.com › tidbits › 94-how-to-check-if-object-is-empty
How to Check if Object is Empty in JavaScript | SamanthaMing.com
It's just regular, plain JavaScript without the use of a library like Lodash or jQuery. We can use the built-in Object.keys method to check for an empty object.
🌐
freeCodeCamp
freecodecamp.org › news › check-if-an-object-is-empty-in-javascript
How to Check if an Object is Empty in JavaScript – JS Java isEmpty Equivalent
November 7, 2024 - For example: let userDetails = { name: "John Doe", username: "jonnydoe", age: 14 }; console.log(JSON.stringify(userDetails)); Output: "{'name':'John Doe','username':'jonnydoe','age':14}" This means when it is an empty object, then it will return ...
🌐
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:
🌐
CoreUI
coreui.io › blog › how-to-check-if-an-array-is-empty-in-javascript
How to check if an array is empty in JavaScript? · CoreUI
February 7, 2024 - An astute question arises: why not rely solely on the length property to determine if an array is empty? The answer lies in JavaScript’s flexibility. Since various data types could technically have a length property, we must first ensure we deal with an array to avoid false positives or negatives. const string = 'Hello World' console.log(string.length) // Output: 11 console.log(Array.isArray(string)) // Output: false · When used on a string, the length property returns the number of characters in the string. In this example, 'Hello World' contains 11 characters, so string.length outputs 11.
🌐
Coderwall
coderwall.com › p › _g3x9q › how-to-check-if-javascript-object-is-empty
How to check if JavaScript Object is empty (Example)
July 27, 2025 - var myObj = { myKey: "Some Value" } if(myObj.isEmpty()) { // Object is empty } else { // Object is NOT empty (would return false in this example) }
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-check-if-object-is-empty
How to Check If an Object Is Empty in JavaScript | Built In
If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty.
🌐
Zipy
zipy.ai › blog › how-do-i-test-for-an-empty-javascript-object
how do i test for an empty javascript object
April 12, 2024 - The keys are strings (or symbols), and the values can be of any data type, including other objects, arrays, or even functions. const person = { name: 'John Doe', age: 30, city: 'New York' }; In the example above, person ...
🌐
Flexiple
flexiple.com › javascript › check-if-object-is-empty
How to Check if an Object is Empty in JavaScript – JS Java isEmpty Equivalent - Flexiple
Unlike other methods that might require polyfills or additional logic, Object.values() works directly and efficiently in modern JavaScript applications. Another example could involve a function that takes an object as a parameter and logs a message based on its emptiness:
🌐
Scaler
scaler.com › home › topics › how to check if an object is empty in javascript?
How to Check if an Object is Empty in JavaScript - Scaler Topics
January 6, 2024 - Underscore is a JavaScript library that provides utility functions for common programming tasks. Underscore provides a similar function to lodash to check if an object is empty. Look at the following example to understand how we can use Underscore to check if an object is empty.