can someone confirm my understanding of this if statement is correct

时间:2019-04-08 13:55:36

标签: javascript if-statement

I'm learning javascript and want to make sure i understand what is happening here.

Is the 'if' part of the statement in my code saying "string" (as in the type of data it is) == "images/card_back.jpg" therefore returns false.

It also returns false if i change == "images/card_back.jpg" to == "string". Is this because although the type of data is "string" it's not the same thing because it's essentially saying data type "string" is the same as the characters "string".

Also is the 'if else' part of the statement using the match() function to find "card" within the "images/card_back.jpg" source character string therefore returns true as card is in that string.

document.getElementById("test").innerHTML = typeof document.getElementById("myImage").src;

if (document.getElementById("myImage").src == "images/card_back.jpg"){
document.getElementById("output 1").innerHTML = "output 1 working"
} else if(document.getElementById("myImage").src.match("card")){
document.getElementById("output 2").innerHTML = "output 2 working"
}else {
}
<!DOCTYPE html>
<html>
<head>
</head>

<body>
<p id="test"></p>

<p id="output 1"></p>
<p id="output 2"></p>


<img id="myImage" src="images/card_back.jpg" width="80" height="80">


</body>
</html>

I was going to attach an image saved on my computer showing what the code outputs but not sure how to do this here.

The code outputs:

string

output 2 working

(an image of the back of a card)

0 个答案:

没有答案