PDA

View Full Version : What is the difference if("test") and if(!!"test")



anya
3 Aug 2010, 7:13 PM
What is the difference if("test") and if(!!"test"), only judged the false or true;

brian.moeskau
3 Aug 2010, 7:38 PM
JavaScript is uses "truthy" and "falsey" boolean values. E.g., a zero, empty string, null, etc can all be interpreted as false (while any non-falsey value is likewise truthy). if(x) simply means that x is some value that is not falsey (could be almost anything). If you want to know that x is literally the boolean value true, you can use the !! operator to essentially type cast the variable to boolean true or false.

jarrednicholls
3 Aug 2010, 8:40 PM
Interesting that this is posted on the Ext Designer help forum :-)



If you want to know that x is literally the boolean value true, you can use the !! operator to essentially type cast the variable to boolean true or false.


I would amend that statement by saying...if you want to type cast any value to a boolean true or false, you can use the double-negative approach of !!. But if you want to see if "x" is literally the boolean value true, you want to test if (x === true) using the === operator.

anya
3 Aug 2010, 9:15 PM
thanks for your answer