Jump to content

if else with a string test fail


shaijuelayidath

Recommended Posts

In the if else tutorial, when I used a condition (10>5) it is working, but when i placed a name (string) it is not working. Code given below <script type="text/javascript">if(x="Johny"){a="Welcome Johny";}else{a="Welcome New User";}document.getElementById("on").innerHTML=a;</script> If i make any change in the name "Johny", it should'nt be show "Welcome New User" ?

Link to comment
Share on other sites

The usual comparison error: You're assigning the value "Johnny" to x then testing whether that is true. It is always true because a non-empty string is a non-false value. To perform comparisons, use the double-equals, comparison operator ==

Link to comment
Share on other sites

You will also see the condition test operator === in some javascript code.=== IS DIFFERENT from ==. == will test if x is equal to y. It doesn't care if x is a string or integer, nor does it care about y.However, === will test if x is equal to y, but it does care if x and y are strings or integers. If x = "5"; and y = 5; and you test x == y, it will come out true. But if you test x===y it will be false because they are different data types. ~Krewe

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...