Search code examples
javascriptunary-operator

An example of a logical opposite value of a variable in javascript


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?


Solution

  • 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);