Page 1 of 1

when false is ! false.

Posted: Tue Mar 31, 2009 9:16 pm
by php_east
this works,

Code: Select all

        if (document.stage_on=='undefined') document.stage_on   = false;
        if (!document.stage_on) 
            {
            stage.innerHTML = frame_body;
            document.stage_on = true;
            }       
        else 
            {
            stage.innerHTML = '';
            document.stage_on = false;
            }       
 
this doesn't

Code: Select all

        if (document.stage_on=='undefined') document.stage_on   = false;
        if (document.stage_on==false) 
            {
            stage.innerHTML = frame_body;
            document.stage_on = true;
            }       
        else 
            {
            stage.innerHTML = '';
            document.stage_on = false;
            }       
 
and i cannot explain.

p/s i'm almost certain it's a javascript bug.

Re: when false is ! false.

Posted: Wed Apr 01, 2009 12:09 am
by novice4eva

Code: Select all

 
#  if (document.stage_on=='undefined') document.stage_on   = false;
#         if (document.stage_on!=false)//NOT document.stage_on==false since you were doing NOT FALSE check in the prior
#             {
#             stage.innerHTML = frame_body;
#             document.stage_on = true;
#             }      
#         else
#             {
#             stage.innerHTML = '';
#             document.stage_on = false;
#             }       
 

Re: when false is ! false.

Posted: Wed Apr 01, 2009 8:08 am
by php_east
if x = false. ( which it is )

(NOT x) == true. TRUE
x==false. TRUE ( it should be the same ).

so, if (!document.stage_on) evalutes to TRUE.
then (document.stage_on==false) should also evaluate to true.


look at my codes again.

Code: Select all

 
 if (!document.stage_on) 
            {
 

Code: Select all

 
        if (document.stage_on==false) 
            {
 
document.stage_on IS false. so how now ?

Re: when false is ! false.

Posted: Wed Apr 01, 2009 12:19 pm
by kaszu

Code: Select all

if (document.stage_on=='undefined')
evaluates to false, since your not checking type, but value

Code: Select all

if (document.stage_on!=false)
is false, because your checking "undefined != false" and they are not equal (not sure why they aren't), but

Code: Select all

if (!document.stage_on)
is true.

Correct is:

Code: Select all

if (document.stage_on === undefined)

Re: when false is ! false.

Posted: Wed Apr 01, 2009 1:41 pm
by php_east
ok, thanks, i will confirm later. at present i am using if (!document.stage_on) but i wish to know why because i don't want to make the same error elsewhere.

Re: when false is ! false.

Posted: Wed Apr 01, 2009 2:13 pm
by php_east
kaszu wrote:Correct is:

Code: Select all

if (document.stage_on === undefined)
no, this is not right, but your explanation is right.

for the records sake, the problem is that i am not checking the type, but the value as you said, and so this code below corrects that

Code: Select all

if (typeof (document.stage_on)=='undefined') document.stage_on  = false;
after that the value of document.stage_on is false, which then can be tested with
if (document.stage_on == false)
or
if (!document.stage_on)

which will then both evaluate to true.

tricky, but solved. thanks.

Re: when false is ! false.

Posted: Wed Apr 01, 2009 2:22 pm
by josh
Although it would be cool if you could do:

if( var a = some.expression() )
{
// do stuff with a
}

Re: when false is ! false.

Posted: Wed Apr 01, 2009 2:31 pm
by php_east
agreed, but it would make my codes shorter and i will not be able to charge more :( :D ( just joking ! )

Re: when false is ! false.

Posted: Wed Apr 01, 2009 4:34 pm
by kaszu
Don't understand why

Code: Select all

if (document.stage_on === undefined)
is not right? 8O

Re: when false is ! false.

Posted: Wed Apr 01, 2009 4:54 pm
by php_east
very sorry, my mistake, that works yes, it is my brain that stopped functioning. i'm going back to defrag it.

if (document.stage_on === undefined)
or
if (document.stage_on == undefined)
or
if (typeof document.stage_on == 'undefined' )


all works, but yours is cleaner so i will use that. thanks.