LarsDenBakker/lit-html-examples

Conditional logic: `Bool ? 'str' : ''` can be simplified to `Bool && 'str'`

kyleoliveiro opened this issue · 4 comments

const message = 'hello friend';
const showMessage = true;

${showMessage
    ? html`The message is: ${message}`
    : ''}

// The above is the same as:

${showMessage && html`The message is ${message}`}
motss commented

@kyleoliveiro Is this a question? This does not work as expected. It will returned undefined if it evaluates to false.

@motss Whoops, was supposed to be a suggestion. But you're right. It returns false if showMessage evaluates to false.

@kyleoliveiro You might be right, I think lit-html doesn't render undefined so yours might be a shorter syntax

I tested, but it renders out "false" if showMessage evals to false. Sorry for the confusion.