Tell us what’s happening:
Describe your issue in detail here.
Hi all I was completing the Diff Two Arrays challenge and was looking at the sollutions. In solution two it used ‘!arr’ - My Question is what does the ‘!’ operator do in this situation. Thanks
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36
the ! operator is called the “logical NOT”, meaning whatever expression prefixed by ! has its boolean value flipped to the opposite.
e.g. !true is false, !false is true.
I looked through the solution, and what actually was written was !arr.includes(), which means reverse the boolean return value of arr.includes(). Relevant MDN below:
You are reading that wrong.
Generally the ! inverts the value of a boolean. So true becomes false and false becomes true.
So in the solution, it refers not to the arr itself, but there is some arr.method() which returns a boolean value and that one is getting negated → so read it as logical “not”.
Sidenote: “!arr” on an array would actually be “false” because boolean-checks consider an array as “truthy” and thus the negation is ALWAYS “false”. So it would be nonsens to just use it on an array itself.