Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript TRUE is not defined or in quotes

I have a XML file that contains

<car>
    <id>123</id>
    <sunroof>FALSE</sunroof>
    <service>TRUE</service>
</car>

The following code only works if I wrap TRUE inside quotes e.g (service == "TRUE")

var service = tis.find("service").text();

if(service === TRUE){
    var service_tag = '<a title="Service" href="">Service</a>'
} else {
    var service_tag = '';
}
like image 321
John Magnolia Avatar asked Oct 04 '11 16:10

John Magnolia


3 Answers

Without quotes javascript will try to interpret TRUE as a value / expression. There is no value TRUE natively defined in javascript. There is true but javascript is case sensitive so it won't bind TRUE to true.

The value you get back from text() is a string primitive. Writing "TRUE" gives you back the string "TRUE" which does compare succesfully with the value service

like image 107
JaredPar Avatar answered Oct 01 '22 13:10

JaredPar


JavaScript boolean true and false are lower case.

like image 25
MeLight Avatar answered Oct 01 '22 13:10

MeLight


Set service equal to this, so JavaScript will be able to interpret your values:

var service = tis.find("service").text().toLowerCase(); 
like image 25
WEFX Avatar answered Oct 01 '22 11:10

WEFX