0

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")
1
  • There's no reason to not use !=. Also, one should use !== and === when possible (almost always, usually). Commented May 22, 2012 at 21:25

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It would also have been better to use === (and !=== in your rewrite).
Yes; without knowing what "this.variable" is, it's hard to say whether an "==" comparison would be correct. (It probably would be, but possibly not.)
Nope. I never want a == comparison. I'll coerce things explicitly if I want coercion, and leave off the relational entirely if I only care about Boolean interpretation.
Well @MarkReed, that's a fine thing if it's what you want to do, but not everybody codes that way.
0
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.

Comments

0

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.

Comments

0

It's returning a true or false...

the exclamation point is a NOT...

so this.variable not equal to "value".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.