Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support && operator-based conditionals in expressions

See original GitHub issue

What do you think of supporting && operator-based conditionals in interpolated expressions?

Current syntax:

html`<input type="checkbox" name="reviewed" ${data.reviewed ? 'checked' : ''} />`

Proposed syntax to support:

html`<input type="checkbox" name="reviewed" ${data.reviewed && 'checked'} />`

The proposed syntax is simply more succinct, and I believe it’s popular in the react ecosystem. While the above example doesn’t save much text, the : '' gets in the way more when you have multi-line expressions – it becomes a trailing piece that is often overlooked.

Currently, the proposed syntax works in bel if the first condition is truthy, but if it is falsy, bel renders its falsy value, and you’d end up with <input type="checkbox" name="reviewed" undefined />. I believe the way around this would be for bel (or maybe hyperx?) to not return anything from an interpolated expression that evaluates to undefined or false. I believe this is how JSX does it.

If for some reason you want to render undefined or false, I think it would be reasonable to convert it to a string (since it’s a string once it’s in the DOM anyway). I think the exception to this would be the value 0 – it may be unexpected to have to convert 0 to a string just to render it to the DOM. 0 comes in to play when you want to do ${rows.length && 'foo'} vs. ${rows.length > 0 && 'foo'}.

/cc @yoshuawuyts

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:6
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

yoshuawuytscommented, Oct 2, 2016

I’m on board with this; seems useful enough and can’t think of a reason to render the value undefined like ever

goto-bus-stopcommented, Jun 21, 2019

@mjstahl yeah, hyperx is not an easy codebase. i’d have to go in with some console.logs myself to be sure, but I think the places to look at would be the places that call strfn and concat. Those functions are used to stringify text and attribute values and to concatenate them if there is a ${templatePart} inside.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional (ternary) operator - JavaScript - MDN Web Docs
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?)
Read more >
the ternary conditional operator - Microsoft Learn
Learn about the C# ternary conditional operator, (`?:`), that returns the result of one of the two expressions based on a Boolean ...
Read more >
Conditional expressions - IBM
Conditional expressions are used in combination with the IF and DO statements to manipulate and select data in the Job Activity section. When...
Read more >
Conditional Operators - Oracle Help Center
Condition can be any valid condition applied to the complex conditions And, Not, and Or. These operators can have embedded conditions. (And, Not,...
Read more >
Conditional Statements in Python
Python supports one additional decision-making entity called a conditional expression. (It is also referred to as a conditional operator or ternary operator in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found