Learn Basic String and Array Methods by Building a Music Player - Step 61

Tell us what’s happening:

Step 61
What do you mean by ternary operator?

Your code so far

// display current song title and artist
const setPlayerDisplay = () => {
  const playingSong = document.getElementById("player-song-title");
  const songArtist = document.getElementById("player-song-artist");
  const currentTitle = userData?.currentSong?.title;
  const currentArtist = userData?.currentSong?.artist;

  if (currentTitle) {
    playingSong.textContent = currentTitle;
  } else {
    playingSong.textContent = "";
  }
  if (currentArtist) {
    songArtist.textContent = currentArtist;
  } else {
    songArtist.textContent = "";
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Learn Basic String and Array Methods by Building a Music Player - Step 61

A ternary operator is a shortcut for an if…then…else statement.
An example of if…then…else

if a < b {
  c = d
}
else {
  c = e
}

This can be done using a ternary operator, like so

c = a < b ? d : e

The condition comes first, then a question mark, then the return portion if true, then the return portion if false

Hope this helps

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.