0

Yesterday it have read this blog-article by John Resig: http://ejohn.org/blog/comparing-document-position/

This snippet I don't understand:

function contains(a, b){
  return a.contains ?
    a != b && a.contains(b) :
    !!(a.compareDocumentPosition(b) & 16);
}

The first expression is clear: If a isn't identical with b and b is contained in a then return true. But what is the purpose of the two-times negation in the second expression? The return of compareDocumentPosition() is bitwise compared with 16. => Results either in 16 or 0. Let's say it is 16 then it becomes false after the first negation. Then true again after the second negation. In case of 0 it becomes true and then false again.

Does anyone understand the code and can explain it?

michael.zech
  • 3,524
  • 5
  • 19
  • 37

1 Answers1

1

The double negation operator is used to convert as boolean.. think of it as a similar method to parseInt()

Here's a related stackoverflow question

Community
  • 1
  • 1
Jad Joubran
  • 2,339
  • 3
  • 27
  • 57