Does javascript treat empty string as either a falsy or null value, and if so why?

Yes it does, and because the spec says so (§9.2).

Isn't an empty string still an object

No. An primitive string value is no object, only a new String("") would be (and would be truthy)

Answer from Bergi on Stack Overflow
🌐
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.

Discussions

javascript - Falsy values vs null, undefined, or empty string - Software Engineering Stack Exchange
If fields should be a string, then !fields is a sufficient predicate. If fields is an array, your best check might be: ... No, they are not the same. ... if ((fields === null) || (fields === undefined) || (fields === 0) || (fields === '') || (fields === NaN) || (fields === flase)) { ... } ... Let's first talk about truthy and falsy values. It's all about what happens when you evaluate something as a boolean. In JavaScript... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
In javascript, is an empty string always false as a boolean? - Stack Overflow
in javascript, var a = ''; var b = (a) ? true : false; var b will be set to false. is this a defined behavior that can be relied upon? More on stackoverflow.com
🌐 stackoverflow.com
Problem with NULLs being turned into empty strings in Select in Form, (setting default in Select helped)
Summary Retool turns null values into empty strings even if their type is not string. This causes a type mismatch when writing the data back to the database. More details I'm sure I've written about this issue before in connection with columns of type DATE. I can't find that comment right now, ... More on community.retool.com
🌐 community.retool.com
1
0
June 17, 2025
Why is my non-empty string evaluating to false?
When I run this code in the JS console, it evaluates to false. I don't get it. I know empty strings are false, but not non-empty strings. More on teamtreehouse.com
🌐 teamtreehouse.com
2
September 18, 2014
🌐
Nfriedly
nfriedly.com › techblog › 2009 › 07 › advanced-javascript-operators-and-truthy-falsy
Advanced Javascript: Logical Operators and truthy / falsy
When javascript is expecting a boolean and it’s given something else, it decides whether the something else is “truthy” or “falsy”. An empty string (''), the number 0, null, NaN, a boolean false, and undefined variables are all “falsy”. Everything else is “truthy”.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Falsy
Falsy - Glossary | MDN
July 11, 2025 - Examples of falsy values in JavaScript (which are coerced to false in Boolean contexts, and thus bypass the if block): ... if (false) { // Not reachable } if (null) { // Not reachable } if (undefined) { // Not reachable } if (0) { // Not reachable } if (-0) { // Not reachable } if (0n) { // Not reachable } if (NaN) { // Not reachable } if ("") { // Not reachable } If the first object is falsy, it returns that object:
Top answer
1 of 5
24

In programming, truthiness or falsiness is that quality of those boolean expressions which don't resolve to an actual boolean value, but which nevertheless get interpreted as a boolean result.

In the case of C, any expression that evaluates to zero is interpreted to be false. In Javascript, the expression value in

if(value) {
}

will evaluate to true if value is not:

null
undefined
NaN
empty string ("")
0
false

See Also
Is there a standard function to check for null, undefined, or blank variables in JavaScript?

2 of 5
9

The set of "truthy" and "falsey" values in JavaScript comes from the ToBoolean abstract operation defined in the ECMAScript spec, which is used when coercing a value to a boolean:

+--------------------------------------------------------------------------+
| Argument Type | Result                                                   |
|---------------+----------------------------------------------------------|
| Undefined     | false                                                    |
|---------------+----------------------------------------------------------|
| Null          | false                                                    |
|---------------+----------------------------------------------------------|
| Boolean       | The result equals the input argument (no conversion).    |
|---------------+----------------------------------------------------------|
| Number        | The result is false if the argument is +0, −0, or NaN;   |
|               | otherwise the result is true.                            |
|---------------+----------------------------------------------------------|
| String        | The result is false if the argument is the empty String  |
|               | (its length is zero); otherwise the result is true.      |
|---------------+----------------------------------------------------------|
| Object        | true                                                     |
+--------------------------------------------------------------------------+

From this table, we can see that null and undefined are both coerced to false in a boolean context. However, your fields.length === 0 does not map generally onto a false value. If fields.length is a string, then it will be treated as false (because a zero-length string is false), but if it is an object (including an array) it will coerce to true.

If fields should be a string, then !fields is a sufficient predicate. If fields is an array, your best check might be:

if (!fields || fields.length === 0)
🌐
freeCodeCamp
freecodecamp.org › news › falsy-values-in-javascript
Falsy Values in JavaScript
December 14, 2019 - Description A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsey values in JavaScript: undefined, null, NaN, 0, "" (empty string), and false of course. Checking for falsy values ...
🌐
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
function isEmpty(value) { return (value == null || (typeof value === "string" && value.trim().length === 0)); } console.log(isEmpty("cat")); // false console.log(isEmpty(1)); // false console.log(isEmpty([])); // false console.log(isEmpty({})); // false console.log(isEmpty(false)); // false console.log(isEmpty(0)); // false console.log(isEmpty(-0)); // false console.log(isEmpty(NaN)); // false console.log(isEmpty("")); // true console.log(isEmpty(" ")); // true console.log(isEmpty(null)); // true console.log(isEmpty(undefined)); // true
Find elsewhere
🌐
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.
🌐
SitePoint
sitepoint.com › blog › javascript › truthy and falsy values: when all is not equal in javascript
Truthy and Falsy Values: When All is Not Equal in JavaScript — SitePoint
November 11, 2024 - For example, the number 0 is falsy, but the string “0” is truthy. This is because an empty string is falsy, but a non-empty string, even if it contains a character that represents a falsy value, is truthy.
🌐
Sololearn
sololearn.com › en › Discuss › 1779570 › is-an-empty-string-true-or-false-in-python-3
Is an empty string true or false in python 3 | Sololearn: Learn to code for FREE!
April 29, 2019 - This is the code I have made to check it but I don't get it still... https://code.sololearn.com/cAV8IGGRwinU/?ref=app When I check that ''==False it returns false. But
🌐
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 - Here are some best practices to follow when checking for empty or null strings in JavaScript: Always use triple equals (===) when comparing a string to null. This ensures that the types are checked, and you don't accidentally compare a 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.
🌐
W3Schools
w3schools.com › js › js_booleans.asp
JavaScript Booleans
The Boolean value of "" (empty string) is false: let x = ""; Boolean(x); Try it Yourself » · The Boolean value of undefined is false: let x; Boolean(x); Try it Yourself » · The Boolean value of null is false: let x = null; Boolean(x); Try ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Type Conversions
January 24, 2023 - Some languages (namely PHP) treat "0" as false. But in JavaScript, a non-empty string is always true.
🌐
Squash
squash.io › how-to-check-for-an-empty-string-in-javascript
How To Check For An Empty String In Javascript
September 5, 2023 - Otherwise, it returns false. This approach is straightforward and works well for most scenarios where you want to check for an empty string. However, it's worth noting that this method will also consider a string consisting of whitespace characters ...
🌐
Trevor Lasn
trevorlasn.com › blog › javascript-truthy-and-falsy
JavaScript Truthy and Falsy: A Deep Dive
November 1, 2024 - ... The empty array comparison [] == false evaluates to true, yet an empty array is actually truthy. This happens because JavaScript first converts the array to a primitive value.