when false is ! false.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

when false is ! false.

Post 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.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: when false is ! false.

Post 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;
#             }       
 
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: when false is ! false.

Post 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 ?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: when false is ! false.

Post 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)
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: when false is ! false.

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: when false is ! false.

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: when false is ! false.

Post by josh »

Although it would be cool if you could do:

if( var a = some.expression() )
{
// do stuff with a
}
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: when false is ! false.

Post by php_east »

agreed, but it would make my codes shorter and i will not be able to charge more :( :D ( just joking ! )
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: when false is ! false.

Post by kaszu »

Don't understand why

Code: Select all

if (document.stage_on === undefined)
is not right? 8O
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: when false is ! false.

Post 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.
Post Reply