I want to display hello username if the condition isLoggedIn is true, and just hello otherwise.
I use ES6 line patterns to try to make the syntax more enjoyable than just concatenating old lines. However, the best way I've found is this ( es6fiddle )
'hello ${isLoggedIn(false) ? username : ''}'
This is still not very good syntax, especially in long templates where there is more than one variable with which I want to do this.
I tried to find syntax that included strings, optionally, in condition-based patterns, but I don't think it is. Something like this would be better:
'hello ${condition && username}'
But it returns false to the string if the condition is false.
I also tried to insert the truth of the username into the variable itself, that is, have username undefined or null if it does not exist - however, the string template then simply displays undefined or null .
Can anyone recommend a nicer syntax or approach, or is this the first method that I will best do with String patterns?
source share