Reputation: 3083
I am trying to understand unary operators in javascript, and found this reference guide here http://wiki.answers.com/Q/What_are_unary_operators_in_javascript, I understand most of the examples but I am trying to work out how I would use this:
!a; // Logical opposite of a in an example.
For instance if I do this:
a = true;
!a;
document.writeln(a);
I thought this would be false, but it outputs true.
What is a good code example where I can use something along the lines of !a to see how it works?
Upvotes: 0
Views: 250
Reputation: 3382
the value of !a
is false
, but you don't save the new value of a
, so the outputted a
is still true
.
Try:
document.writeln(!a);
Upvotes: 1
Reputation: 309
a = !a;
You didn't assign the result of !a
into the variable. Try this.
Upvotes: 1
Reputation: 7507
As stated by Evil_skunk, you don't store the value. So you have to do either this:
document.writeln(!a);
or this:
a = !a;
document.writeln(a);
Upvotes: 2
Reputation: 6365
You aren't assigning !a
to any variable. a
is still true.
What you want to do is this,
a = true;
a = !a;
document.writeln(a);
Upvotes: 3