Why does an empty string in a vue template return as truthy?
<div>{{ "" ?? "empty string is not truthy" }}</div>
This will just display the empty string. I have a bunch of empty string fields which I can’t check with the nullish coalescing operator (??) but instead have to use ternaries:
<div>{{ "" ? "string empty" : "string not empty" }}</div>
Shouldn’t "" ??
be faulty?
2
Answers
"" is not null nor undefined. Just like 0. These are just ‘falsey’, so won’t work with
??
:The Nullish coalescing operator (??) tests if a value is not
null
orundefined
, not if it is truthy.""
is a falsy value, but it isn’tnull
and it isn’tundefined
.Use the Logical OR (||) operator to test for truthiness.