What does the exclamation mark do before the function?

后端 未结 9 1894
误落风尘
误落风尘 2020-11-21 04:47
!function () {}();
9条回答
  •  醉梦人生
    2020-11-21 04:49

    Its just to save a byte of data when we do javascript minification.

    consider the below anonymous function

    function (){}
    

    To make the above as self invoking function we will generally change the above code as

    (function (){}())
    

    Now we added two extra characters (,) apart from adding () at the end of the function which necessary to call the function. In the process of minification we generally focus to reduce the file size. So we can also write the above function as

    !function (){}()
    

    Still both are self invoking functions and we save a byte as well. Instead of 2 characters (,) we just used one character !

提交回复
热议问题