aderaaij
11/11/2017 - 5:52 PM

ES6 - Template string with ternary operator

In this template string we use a ternary conditional operator to only show the 'featured' part when it's available. If not available, the last part is omitted. From es6.io

const song = {
    name: 'Dying to live',
    artist: 'Tupac',
    featuring: 'Biggie Smalls'
};

const markup = `
    <div class="song">
        <p>
            ${song.name} — ${song.artist}
            ${song.featuring ? `(Featuring ${song.featuring})` : ''}
        </p>
    </div>
`;