Diff Two Arrays --- Trying to understand the Syntax

Tell us what’s happening:

Hello fellow campers!

I looked up for the solution to this exercise but I am unable to understand the syntax of this solution.

Precisely this part ----------- !arr1.includes(item) || !arr2.includes(item)

Can someone please explain the meaning of ! in “!arr1”.

Thank you.

Your code so far

  function diffArray(arr1, arr2) {
      return arr1
            item => !arr1.includes(item) || !arr2.includes(item)

    console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36`.

**Link to the challenge:**

the ! is the logical NOT operator, it flips the boolean value of whatever it is put in front of
so arr1.includes(item) returns or true or false, and with the ! it will become the opposite value

Thank you for clarifying that.

Could you please explain this code as well?

item => !arr1.includes(item) || !arr2.includes(item)

Thank you.

  • => : Arrow function, equivalent to an anonymous function with a special this context
  • ! : Negates the boolean evaluation of an immediate expression, for example !true equals to false and viceversa.
  • .includes : An ES7 (modern JS) array method to determine if the argument passed to it is present in the list that you’re operating the method on. It’s the same as the old way: arr1.indexOf(item) >= 0 which some people also express it as !== -1 (not equal to minus one).
  • || : Boolean operator “or” which means that at least one of the two expressions should be true for the result to be true, otherwise it yields false (if both operands are false).

In general it reads:

A function that receives an item as an argument and returns the following assessment:

Is the item present in arr1 or present in arr2?

And where does the item come from? From an array that contains all of the objects from the arr1 and arr2

This challenge is supposed to be a set operation so IDK if it passes the challenge at all because a set isn’t supposed to have repeated elements (in case one of the two arrays were to have repeated elements ofc).

1 Like