Best practice to check for booleans?

peterraeves
Mega Guru

I picked up the habit of checking for <variable> == 'true', when checking for variables. Though this got me into trouble once, because someone entered the value 'True', resulting in a false negative. Should I check for <variable> == true instead? Or what is the best practice in ServiceNow for checking for boolean values?

3 REPLIES 3

Chris M3
Tera Guru

JSUtil.toBoolean(value) will evaluate the value for a few different conditions.   You can take a look at the JSUtil script include in your instance to see exactly what the code does.



However, you should be using a check box, or choice box for boolean values, so that is not dependent on the user typing in a value.


The values come in through the web service and there we cannot use anything other than strings


Alikutty A
Tera Sage

I believe this should fall into java script best practices



For boolean values you can directly validate the variable in if condition



var check = true;


if(check){


// execute true conditions


}



if(!check){


// execute false conditions


}




Thanks


PS: Hit like, Helpful or Correct depending on the impact of the response