Why is the empty string considered a truth value in Vue.js templates?
P粉164942791
2023-08-09 18:14:19
<p>Why does the empty string return as a true value in the vue template? </p>
<pre class="brush:php;toolbar:false;"><div>{{ "" ?? "The empty string is not a true value" }}</div></pre>
<p>This will only display empty strings. I have a lot of empty string fields that cannot be checked with the null coalescing operator (??), instead I have to use the ternary operator: </p>
<pre class="brush:php;toolbar:false;"><div>{{ "" ? "String is empty" : "String is not empty" }}</div></ pre>
<p><code>"" ??</code>Shouldn’t it be wrong? </p>
Nullish coalescing operator (??) is used to test whether a value is not equal to
null
orundefined
, rather than determining whether it is a "true value" .""
is a false value, but it is neithernull
norundefined
.Use the logical OR (||) operator to test for "truth".