0

I came across the following syntax in a GTM script and am not quite sure how it's supposed to work:

if(!!{{var1}} && !!{{var2}}){
    // . . . other code here 
}

Now, I can tell that the {{}} syntax is used to capture variable values, but I can't find any reference to the !! operator (if there is such a thing).

Does it represent something in GTM, or is it just sloppy programming and these !! can be removed? I should mention that there are many more if statements in this script and they all look sane. This is the only one that has these !!. Any ideas?

ankush981
  • 5,159
  • 8
  • 51
  • 96
  • Related, although for a different language: https://stackoverflow.com/questions/248693/double-negation-in-c-code (I don't know GMT so cannot be sure it is doing the same thing.) – Willem Renzema Aug 30 '19 at 13:13
  • @WillemRenzema If you don't know GTM why are you bothering? Does it make _any_ sense to draw parallels from a different language? – ankush981 Aug 30 '19 at 13:18

1 Answers1

2

This is not GTM script specific, it's a standard JavaScript practice to coerce objects to boolean. In JavaScript there are falsey values like null or undefined, etc. (list here), these will be false after using !! (bang bang), otherwise, true.

Note that this is not a special operand, just ! (NOT) used twice to force the type to be boolean.

Here is a good article that explains it very well: https://medium.com/@edplatomail/js-double-bang-or-the-not-operator-part-40e55d089bf0

To answer your question, it is not sloppy programming, and should not be removed.

Matus
  • 1,408
  • 12
  • 15