Jamming Task 41 -- using .find() in an 'if' statement

Here is the exercise

I’m going through this part of the question:
Use the track’s id property to check if the current song is in the playlistTracks state.

The solution uses .find() to go through the array and look for an element with an id that matches the parameter track id:

  let tracks = this.state.playlistTracks;
  if (tracks.find( t => t.id === track.id )) {
    return
  }
  tracks.push(track);
  this.setState({
    playlistTracks: tracks
  })
}

My understanding is that .find() returns the first element in the array that matches the condition given, t.id === track.id

Does returning an element satisfy the if statement? I thought that what you put into the if statement had to evaluate to true or false, and that not returning true would mean that the track would always be added to playlistTracks.

1 Like

Hi Andrew,

Since find() returns the value it should evaluate to true as long as the value is not anything in the following:

**Examples of *falsy* values in JavaScript** (which are coerced to false in Boolean contexts, and thus *bypass*  the `if` block):

if (false)
if (null)
if (undefined)
if (0)
if (0n)
if (NaN)
if (’’)
if ("")
if (``)
if (document.all)

You can find more here https://developer.mozilla.org/en-US/docs/Glossary/Falsy

Interesting. And I guess on the flipside, the value that is returned by find evaluates to truthy. That helps, thank you!