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

Tell us what’s happening:

Hi guys, I was wondering if someone could clarify what is actually happening with this code?

I know the answer is:
if (songToHighlight) songToHighlight.setAttribute(“aria-current”, “true”)

But what does this actually mean? Why would you use an if statement for this? So far, I have used if statements to check to see if a condition is true/false. E.G.

if (x === y) {
Execute this code
}

What is an if statement with no comparative condition?

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

l

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

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

This is because JavaScript doesn’t actually require true/false in the condition of if. If there’s something else in the condition, it will be considered whether the value is truthy or falsy.

I think I get it. Just to clarify:

songToHighlight is accessing the currentsong/id property of the currently selected song. This if statement will check to see if this is returning true/false, even if it is not specifically stated? If it returns true (which it will, unless no song is currently selected?), it will set aria-current to true, which will indicate which element is selected and execute the rest of the function, allowing for it to be highlighted?

Sorry for my ignorance. Thanks for the help :slight_smile:

const songToHighlight = document.getElementById(
    `song-${userData?.currentSong?.id}`
  );
/* */

if (songToHighlight) {
  songToHighlight.setAttribute('aria-current', 'true')
}

songToHighlight can point to the element of the currently playing song, or be null (when there’s no song playing). In the second case, trying .setAttribute('aria-current', 'true') method would result in error. Therefore it’s checked if songToHighlight is at that point truthy, in which case attribute can be set.