What does the exclamation mark do before the function?

后端 未结 9 1916
误落风尘
误落风尘 2020-11-21 04:47
!function () {}();
9条回答
  •  猫巷女王i
    2020-11-21 05:14

    ! will negate (opposite) whatever you're expecting as a result, i.e if you have

    var boy = true;
    undefined
    boy
    true
    !boy
    false
    

    when you call boy, your result will be true, but the moment you add the ! when calling boy, i.e !boy, your result will be false. Which in other words you mean NotBoy, but this time it's basically a boolean result, either true or false.

    That's the same thing that happens to the !function () {}(); expression, running only function () {}(); will flag you an error, but add ! right in front of your function () {}(); expression, makes it the opposite of the function () {}(); which should return you true. Example can be seen below:

    function () {}();
    SyntaxError: function statement requires a name
    !function () {}();
    true
    

提交回复
热议问题