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)
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)
Yes an empty string is falsy, however new String("") is not.
Note also that it's well possible that
if (x) { ... }
is verified, but that
if (x == false) { ... }
is verified too (this happens for example with an empty array [] or with new String("")).
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.
javascript - Falsy values vs null, undefined, or empty string - Software Engineering Stack Exchange
In javascript, is an empty string always false as a boolean? - Stack Overflow
Problem with NULLs being turned into empty strings in Select in Form, (setting default in Select helped)
Why is my non-empty string evaluating to false?
Videos
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?
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)
Yes. Javascript is a dialect of ECMAScript, and ECMAScript language specification clearly defines this behavior:
ToBoolean
The result is false if the argument is the empty String (its length is zero); otherwise the result is true
Quote taken from http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
Yes. All false, 0, empty strings '' and "", NaN, undefined, and null are always evaluated as false; everything else is true.
And in your example, b is false after evaluation. (I think you mistakenly wrote true)