Why is the empty string considered a truth value in Vue.js templates?
P粉164942791
P粉164942791 2023-08-09 18:14:19
0
1
512
<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>
P粉164942791
P粉164942791

reply all(1)
P粉776412597

Nullish coalescing operator (??) is used to test whether a value is not equal to null or undefined, rather than determining whether it is a "true value" .

"" is a false value, but it is neither null nor undefined.

Use the logical OR (||) operator to test for "truth".

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template