0

I have a function that's meant to get media playing status from a GET request and do something accordingly depending on whether the media returns playing or not.

My function:

function get_media() {
    fetch('http://127.0.0.1:5000/get_media')
    .then(function(resp) { return resp.json() }) // Convert data to json
    .then(function(data) {
        var songTitle = data.title
        var artist = data.artist
        var playingStatus = data.playing
        console.log(playingStatus)
        if (playingStatus = "true") {
            console.log("playing")
            document.getElementById("play").style.display = "none";
            document.getElementById("pause").style.display = "block";
            document.getElementById('song-title').innerHTML = songTitle;
            document.getElementById('artist-name').innerHTML = artist;
        } 
        if (playingStatus = "false") {
            console.log("paused")
            document.getElementById("play").style.display = "block";
            document.getElementById("pause").style.display = "none";
            document.getElementById('song-title').innerHTML = songTitle;
            document.getElementById('artist-name').innerHTML = artist;
        }
    })
}

The issue is, even if playingStatus is set to false, the function still does the action specified in if (playingStatus = "true"), as shown in this screenshot below of the console.

The console tells me playingStatus is set to false, but is activating the wrong condition in the if statement. Why is this happening?

2
  • 1
    By doing playingStatus = "false", you're assigning the string "false" to playingStatus. To test for equality, use == Commented Apr 10, 2021 at 16:33
  • Use === for comparison. Single = is assignment Commented Apr 10, 2021 at 16:34

2 Answers 2

3

Short answer:

By doing playingStatus = "true" you're assigning a value to playingStatus, not comparing it against the value. If you want to test whether it's a particular value you should use === or ==, as in playingStatus === "true".

Why if (playingStatus = "true") is always true:

Because an assignment expression evaluates to the assigned value, the expression playingStatus = "true" will evaluate to "true", and because "true" is truthy, the condition if (playingStatus = "true") will always be true.

String vs boolean:

Another possible issue is that true and false may not be strings, so you might need to lose the quotes, e.g. playingStatus === true.

Sign up to request clarification or add additional context in comments.

Comments

0

Right, sorry, it turns out I was using = instead of == in my if statement. It should've been if (playingStatus == "false") not if (playingStatus = "false").

Still curious why the first statement returned true, though.

1 Comment

The assignment of "true" to playingStatus is truthy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.