Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is !function in javascript? [duplicate]

I have this code in google tag manager:

<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');

fbq('init', '***********');
fbq('set','agent','tmgoogletagmanager', '***********');
fbq('track', "PageView");
</script>

I just wondered if anyone could tell me what '!function' is, or what it does? I have never seen it before and a google/duckduckgo search produced no results, that I could see.

Could it mean 'not a function'? Weird!

like image 588
Leon Segal Avatar asked Sep 20 '17 11:09

Leon Segal


People also ask

Can set have duplicates in JavaScript?

The Set object lets you store unique values of any type, whether primitive values or object references. you are passing new object not reference so it is allowing to add duplicate.

How to check duplicates in array in JS?

The findDuplicates function (below) compares index of all items in array with index of first occurrence of same item. If indexes are not same returns it as duplicate. This doesn't return a unique array, duplicates will be included more than once if they're duplicated more than once in the array.


1 Answers

It's a shorter method of writing an IIFE:

!function(){console.log('foo')}()

Is 1 character shorter than:

(function(){console.log('foo')})()
like image 107
Cerbrus Avatar answered Oct 19 '22 15:10

Cerbrus