'!' Operator before an array

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

Challenge: Diff Two Arrays

Link to the challenge:

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:

1 Like

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.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.