/ notes
A collection of thoughts, things I've learnt and things I want to remember

JS Snippets

TIL you can use logic, such as a ternary operator, in ES6 template literals (or back tick strings).

For example,

`${result['color 5'] ? 'color 5 exists!' : 'color 5 does not exist!'}`

Which is very handy if you are passing data to a template like string and some of the data is optional - in this case you might not want anything to show up or you can provide a sensible default. For example on gwg,

<div class="gift-description">
  <p>${gift.description ? gift.description : ''}</p>

The MDN documentation mentions you can embed any expression in template literals. So things like functions and even IIFEs.

webdev / js / snippets