سؤال

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