Reputation: 2226
I am editing someone's code and I don't understand what they are trying to do with this statement. This is at the end of the function.
return !(this.variable == "value")
Upvotes: 0
Views: 466
Reputation: 2754
It's returning a true or false...
the exclamation point is a NOT...
so this.variable not equal to "value".
Upvotes: 0
Reputation: 416
They are checking this.variable is equal to "value" (which returns either true or false) and then using the ! (not) to make the value opposite.
It achieves the same result as
return (this.variable != "value")
You might as well change it to that because it is much clearer.
Upvotes: 0
Reputation: 6505
this.variable == "value"
This compares two values and evaluates to a boolean (true if they compare equal).
!(this.variable == "value")
This negates the value (true <-> false).
return !(this.variable == "value")
This returns the value from the function.
Upvotes: 0
Reputation: 413720
They're returning true
or false
based on the opposite of the result of the comparison.
It would probably have been clearer to write:
return this.variable != "value";
Sometimes you see:
return !!(some.expression);
which forces a "truthiness" conversion of the result of the expression to boolean (true
or false
). The "!!" is just a pair of individual logical complement ("not") operators. The first one (on the right) converts the result of the expression to boolean, but the opposite of the "truthiness". The second therefore flips it back.
Upvotes: 6