TwelvestoneFlash

Simple checking boolean question


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
faerieprince
 
2003-01-29

if 'boolean == false', can you also check 'boolean == undefined' or 'boolean == null'?

IE: var dog = false;

if (dog == undefined) {}

  • or -

if (dog == null) {}

Would either of these check to see if 'dog' is set to 'false'?

persist
 
2003-01-29

no false is an existing deifned variable state, so it is neither null nor undefined.

persist
 
2003-01-29

perhaps I misunderstood.

you can indeed declare:

dog = undefined; if (dog == undefined) {trace("OK")}

dog = null; if (dog == null) {trace("OK")}

dog = false; if (dog == false) {trace("OK")}

faerieprince
 
2003-01-29

Sorry for my lack of clarity. What I am asking is this:

Does can you check for a 'false' value by testing against 'null' or 'undefined'? Perhaps this is a better example:

var dog = false; var cat = null;

if (dog == cat) {}

Would the 'if' statement return a true? It seems as though your first reply answered the initial question. Sorry I lagged in getting back to you, but I was in a meeting.

Per usual, thank you for your help!

persist
 
2003-01-29

No it won't return true because a var declared false is no longer null or undefined, but defined as false, so it does indeed exist.

faerieprince
 
2003-01-29

That is what I had thought, but I was not certain. On a related note, does this work:

if (!dog) {}

to test for 'dog == false'?

I had read somewhere that '!variable' does not work consistantly in Flash.

persist
 
2003-01-29

indeed ! will check for false, null, and undiefined.

k

Arsis
 
2003-01-29

_Originally posted by faerieprince _ **That is what I had thought, but I was not certain. On a related note, does this work:

if (!dog) {}

to test for 'dog == false'?

I had read somewhere that '!variable' does not work consistantly in Flash. **

The thread here answeres your questions regarding !variable

http://www.twelvestone.com/forums/showthread.php?s=&threadid=1667

It does work consistantly and is useful once you understand the logic.

jimmyn
 
2003-01-30

i was actually going to offer some thoughts on ! and true/false, then realized that i did so in the thread arsis linked to.

on a side note, another (extremely useful!) use of the ! operator is for creating toggles. let's say you have a boolean that starts at true, and everytime a user hits a button you want to switch it back and forth from true to false to true.....switch = true;

function ToggleSwitch() { switch = !switch; }the function above will always do a logical NOT of the the value of the variable, so if true it becomes false and is false becomes true.

probably my favorite use for ! k

faerieprince
 
2003-01-30

That is pretty smart. I might have to start incorporating that. It is cleaner than my current method of using flags.

Thanks for the idea!

bit-101
 
2003-01-30

one thing that might help you understand it is thinking in data types. there is a type of data called boolean. it has two values, true and false.

whenever you use a comparator (==, !=, <, >, etc.) the result is a boolean value, it's either true/false. also, the argument for if(...) is a boolean value.

so,

flag=true; if(flag)...

is totally valid, as well as

if(!flag)...

because it's looking for a boolean, and it finds one.

saying

if(flag==true)...

is really overkill.

in most other strictly typed languages, data types are much clearer. a boolean can only accept true/false values, and you'd have to declare the variable as being a boolean. flash is so loose and converts so easily, you can often forget that there are different data types, and think that true/false are just other values.

jimmyn
 
2003-01-30

nice explanation bit.

another thought from that. like said above, the comparison operators return boolean values. because of this it is perfectly acceptable to use them to set a boolean. for example, this codeif (myVar > 5) { isGreater = true; } else { isGreater = false; }can be written out like that (and sometimes should be), but you can accomplish the same thing in one line using the comparisons as booleanisGreater = myVar > 5;the code will execute exactly the same in both examples.

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

Simple checking boolean question