W
windandwaves
Is there a way to write this simpler?
function makeOpposite(a) {
if(!a) {
a = true;
}
else {
a = false;
}
}
function makeOpposite(a) {
if(!a) {
a = true;
}
else {
a = false;
}
}
a = !a;windandwaves said:Is there a way to write this simpler?
function makeOpposite(a) {
if(!a) {
a = true;
}
else {
a = false;
}
}
a = !a;
awesome! Thanks Ian...
Or:
a ^= true;
On Feb 15, 4:22 am, John W. Kennedy wrote:
But then - a - is numeric not boolean.
In comp.lang.javascript message <[email protected]
glegroups.com>, Thu, 15 Feb 2007 02:40:40, Richard Cornford
Consequentially and more importantly, if the original a was an integer
greater than 1, then both a and the result would test as true.
Easy enough to fix! Just cast to boolean before and after.
a = !!(!!a ^ true);
Or my favorite js wtf, found in actual production code at a company
where I once worked:
x = (!!x ? !!true : !!false);
Apparently the author wanted to make really super duper certain he got
a boolean.
That approach would not be safe in all languages. ISTR one in which
Boolean was nothing more than a predefined enumerated type in the
outermost scope, so that it would be possible to declare new variables
true and false and assign to them respectively 2>3 and 3>2.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.