By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

does double not(!!) have any significance?

dmjpro
100+
P: 2,476
Yesterday i came across a statement which has double not(!!). What does that mean?
Mar 31 '09 #1
Share this Question
Share on Google+
12 Replies


dmjpro
100+
P: 2,476
Have a look at this.. You will see at the begining ;)
Mar 31 '09 #2

Dormilich
Expert Mod 5K+
P: 8,639
I could imagine, that it forces the object to convert to boolean (a simple ! will convert to the boolean opposite, so !! should convert to the boolean equivalent)
Mar 31 '09 #3

dmjpro
100+
P: 2,476
Have a look at my code.
Expand|Select|Wrap|Line Numbers
  1. function test()
  2. {
  3.  alert(x); //alerts undefined
  4.  alert(!x) //alerts true
  5. }
  6.  
How does !undefined return true?
Mar 31 '09 #4

Dormilich
Expert Mod 5K+
P: 8,639
@dmjpro
undefined is the same as null and the boolean equivalent of that is false, thus the negation of it returns true.
Mar 31 '09 #5

dmjpro
100+
P: 2,476
Actually what happened....Earlier i tried to do if(object_detection), somehow i got error. Then i started to check like this if(typeof object !='undefined' ).
But the error came from somewhere else ;)

Just now i checked if(object_detection)..it's working.
That's why i asked why !undefined returns true :)
Anyway thanks ..for last to days i learned a lot JavaScript basics ;)
Mar 31 '09 #6

dmjpro
100+
P: 2,476
@Dormilich
Today i came across my earlier problem what forced me to have a such Concept of Undefined Object.
You told me that Undefined is equivalent to Null.
If i do like this if(undefined) then it returns if(false), right?
Have a look at my code.

Expand|Select|Wrap|Line Numbers
  1. function test(){
  2.  if(a) alert('Yahoo!!');
  3.  else alert(':(');
  4. }
  5.  
Here an error come up..a is Undefined.
Have a look at my second code.

Expand|Select|Wrap|Line Numbers
  1. function Test(){
  2. }
  3.  
  4. function test(){
  5.  var t = new Test;
  6.  if(t.a) alert('Yahoo!!');
  7.  else alert(':(');
  8. }
  9.  
It's working ... what's an error with previous one?
Apr 4 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
I can only hazard a guess… maybe it's because "a" is a variable and "t.a" is a property (which could be described as the return value of the object's get method).
Apr 4 '09 #8

dmjpro
100+
P: 2,476
@Dormilich
But a also is bound to the window object. ;)
Apr 4 '09 #9

Dormilich
Expert Mod 5K+
P: 8,639
therefore I hazarded the guess...
Apr 4 '09 #10

gits
Expert Mod 5K+
P: 5,390
not so simple :) you would need to qualify the reference ... when you use window.a in your first example it works too, and i think dormilich's explaination is correct ... when you really qualify the reference to a property the parser could know where to look at but with a full unqualified reference-name it is lost, of course it could climb up to the window but when should it do so, always? should it stop in some object-context where you want to have that local? just always declare the variables and everything is determined, and parseable and predictable ... and: i wouldn't check the way you did ... you could always do:

Expand|Select|Wrap|Line Numbers
  1. if (typeof a != 'undefined') {
  2.     // do something
  3. }
this is a secure check ...

kind regards
Apr 5 '09 #11

dmjpro
100+
P: 2,476
Expand|Select|Wrap|Line Numbers
  1. if (typeof a != 'undefined') {
  2.     // do something
  3. }
this is a secure check ...

kind regards
That's what i always been checking but suddenly i came to know if i check an object existence using full qualified name then it works, not need to check like you ;)
Apr 6 '09 #12

gits
Expert Mod 5K+
P: 5,390
it's just good practice to use always use the typeof check to check for existance ;)

kind regards
Apr 6 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.