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?

playingStatus = "false", you're assigning thestring "false"toplayingStatus. To test for equality, use=====for comparison. Single=is assignment