17

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 = '';
}
John Magnolia
  • 16,769
  • 36
  • 159
  • 270

8 Answers8

29

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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
9

JavaScript boolean true and false are lower case.

MeLight
  • 5,454
  • 4
  • 43
  • 67
3

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

var service = tis.find("service").text().toLowerCase(); 
WEFX
  • 8,298
  • 8
  • 66
  • 102
1

I had this error because I quickly typed it with typo

someBooleanVar === ture

this is wrong

it should be true not ture

Basheer AL-MOMANI
  • 14,473
  • 9
  • 96
  • 92
1

its because the tripe equal also check for type, and TRUE it's a identifier "TRUE" is a value

// this will work
if(service === "TRUE"){
    var service_tag = '<a title="Service" href="">Service</a>'
} else {
    var service_tag = '';
}

Difference between == and === in JavaScript

Community
  • 1
  • 1
Roberto Alarcon
  • 1,430
  • 2
  • 18
  • 32
  • 2
    ha, this is not what he/she was asking, the real issue was miss spelling, its `true` not `True` – Ace Mar 30 '20 at 09:31
1

This is expected.

tis.find("service").text(); returns a string, not a boolean, and the JavaScript boolean for truth is true (which is case sensitive, like everything else in the language).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1
var service = tis.find("service").text();

This returns a string "TRUE". Since === checks for the type as well, it always returns false.

1

TRUE refers to a variable named TRUE which doesn't exist, so you get an error. "TRUE" is a string containing the characters TRUE. Your variable service will contain a string, so the second of these are what you want.

Andrew Wilkinson
  • 10,682
  • 3
  • 35
  • 38