Home  >  Q&A  >  body text

Why is the empty string considered a truth value in Vue.js templates?

<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粉164942791P粉164942791459 days ago504

reply all(1)I'll reply

  • P粉776412597

    P粉7764125972023-08-10 09:55:56

    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".

    reply
    0
  • Cancelreply