Reversing a boolean in JS

Got a question regarding JS in squiffy (or whatever that four-space code is called). Anyway, I want to set a boolean opposite from another, i.e. flip it. So the code I have looks something like this...

    set("BoolA",not(get("BoolB")));

...which doesn't work. I could really do it with two lines but I'd like to do this clean and learn a new trick. So, how do you not() something?


And, with some digging, now I know...

    set("BoolA",!(get("BoolB")));

...which is a neat trick.


May be worth noting that javascript's !, && and || are operators (like + and *). So you could shorten that line to:

    set("BoolA", !get("BoolB"));

(Also, if you use ! on something that isn't a boolean, it will convert it to one first. The number zero, an empty string, or an empty list are considered to be false; and most other things are true (I think the string "0" might also be false, but not sure of the exact list. Because ! always gives you a boolean, some programmers have found that !! is the quickest way to convert a number to a boolean)

((random tangent; but I think that's a pretty interesting quirk of the language)


That's one of those tricks I used to dread finding in my coding days - where someone uses some esoteric magic spell. I once crashed a production system with a patch because of a weird little unix script deal (uncommented, of course :) )

But thanks for the followup!


Don't get me started on Perl.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums