문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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);
a = !a;

You didn't assign the result of !a into the variable. Try this.

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top