Expressions

Chapter 5 is about what a template can say. It starts with the braces: anything between { and } is a JavaScript expression, evaluated against what data() returned.

  • Paths: { album.title }, { albums[0].artist }.
  • Operators and ternaries: { album.price * 2 }, { albums.length > 0 ? 'Browse' : 'Empty' }.
  • String, list and number methods: { album.genre.toUpperCase() }, { album.released.slice(0, 4) }, { tags.join(', ') }, { album.price.toFixed(2) }.

A few things are left out on purpose, and the compiler says what to use instead. There is no this (every value comes from data()), no window or document, no new, and no methods that change a list in place: toSorted() instead of sort(), concat() instead of push(). Reading through a missing value never throws: { album.label.name } prints nothing when an album has no label.

Albums now carry more fields: a released date, a stock count, a rating and some notes. They are in the model and in data/albums.js.

Your turn

On the shop's cards, uppercase the genre and put the year next to the title. released is a YYYY-MM-DD string, so the year is its first four characters:

<strong>{ album.title } · { album.released.slice(0, 4) }</strong>
<span>{ album.artist } · { album.genre.toUpperCase() }</span>

Compute anything bigger in data() and print the result. A template is for reading values, not for working them out.

Chapters