I found this code:
!function () {}();
What is the purpose of the exclamation mark here?
I found this code:
!function () {}();
What is the purpose of the exclamation mark here?
JavaScript syntax 101: here is a function declaration:
function foo() {}
Note that there’s no semicolon; this is just a function declaration. You would need an invocation, foo()
, to actually run the function.
Now, when we add the seemingly innocuous exclamation mark: !function foo() {}
it turns it into an expression. It is now a function expression.
The !
alone doesn’t invoke the function, of course, but we can now put ()
at the end: !function foo() {}()
, which has higher precedence than !
and instantly calls the function.
function foo() {}()
would be a syntax error because you can’t put arguments (()
) right after a function declaration.
So what the author is doing is saving a byte per function expression; a more readable way of writing it would be this:
(function(){})();
Lastly, !
makes the expression return a boolean based on the return value of the function. Usually, an immediately invoked function expression (IIFE) doesn’t explicitly return anything, so its return value will be undefined
, which leaves us with !undefined
which is true
. This boolean isn’t used.
(function(){alert('bla');)})();
one can't simply write alert('bla');
? –
Kenlay !function
or }()
other than an iife it makes it easy to scrape them out of code if need be. Not a major thing, but I've used that fact a few times. –
Haddad var foo =
breaks the statement/expression ambiguity and you can simply write var foo = function(bar){}("baz");
etc. –
Flynt !
is a prefix unary operator, and will only ever be interpreted as that. I doubt it would take any competent developer "hours to understand" this. –
Sinnard (function(){})()
means. –
Sinnard function foo() {}
is not a statement. It's a declaration. Though some implementations incorrectly allow it to be used as a statement in some cases. The !function foo() {};
is an expression statement. –
Sheena (function (){}());
–
Claxton WARNING - [JSC_USELESS_CODE] Suspicious code. The result of the 'not' operator is not being used.
–
Gerladina The function:
function () {}
returns nothing (or undefined).
Sometimes we want to call a function right as we create it. You might be tempted to try this:
function () {}()
but it results in a SyntaxError
.
Using the !
operator before the function causes it to be treated as an expression, so we can call it:
!function () {}()
This will also return the boolean opposite of the return value of the function, in this case true
, because !undefined
is true
. If you want the actual return value to be the result of the call, then try doing it this way:
(function () {})()
!
is to turn the function declaration into a function expression, that's all. –
Strawberry !function
syntax –
Dormitory (function () {})()
". Why does that work like that? Thanks, I know this is from forever ago.. –
Gallardo function myFunction(){}; myFunction();
you can just say (function () {})()
. This works because function names are optional and putting brackets around a function declaration make that function an expression, i.e. allow it to be immediately invoked. I know your question is now from forever ago –
Ruinous There is a good point for using !
for function invocation marked on airbnb JavaScript guide
Generally idea for using this technique on separate files (aka modules) which later get concatenated. The caveat here is that files supposed to be concatenated by tools which put the new file at the new line (which is anyway common behavior for most of concat tools). In that case, using !
will help to avoid error in if previously concatenated module missed trailing semicolon, and yet that will give the flexibility to put them in any order with no worry.
!function abc(){}();
!function bca(){}();
Will work the same as
!function abc(){}();
(function bca(){})();
but saves one character and arbitrary looks better.
And by the way any of +
,-
,~
,void
operators have the same effect, in terms of invoking the function, for sure if you have to use something to return from that function they would act differently.
abcval = !function abc(){return true;}() // abcval equals false
bcaval = +function bca(){return true;}() // bcaval equals 1
zyxval = -function zyx(){return true;}() // zyxval equals -1
xyzval = ~function xyz(){return true;}() // your guess?
but if you using IIFE patterns for one file one module code separation and using concat tool for optimization (which makes one line one file job), then construction
!function abc(/*no returns*/) {}()
+function bca() {/*no returns*/}()
Will do safe code execution, same as a very first code sample.
This one will throw error cause JavaScript ASI will not be able to do its work.
!function abc(/*no returns*/) {}()
(function bca() {/*no returns*/})()
One note regarding unary operators, they would do similar work, but only in case, they used not in the first module. So they are not so safe if you do not have total control over the concatenation order.
This works:
!function abc(/*no returns*/) {}()
^function bca() {/*no returns*/}()
This not:
^function abc(/*no returns*/) {}()
!function bca() {/*no returns*/}()
!
does not force a return value; it negates it. If nothing is explicitly returned, functions return undefined
. –
Distinction It returns whether the statement can evaluate to false. eg:
!false // true
!true // false
!isValid() // is not valid
You can use it twice to coerce a value to boolean:
!!1 // true
!!0 // false
So, to more directly answer your question:
var myVar = !function(){ return false; }(); // myVar contains true
Edit: It has the side effect of changing the function declaration to a function expression. E.g. the following code is not valid because it is interpreted as a function declaration that is missing the required identifier (or function name):
function () { return false; }(); // syntax error
var myVar = !function(){ return false; }()
could omit the !
like var myVar = function(){ return false; }()
and the function will execute correctly and the return value will be untouched. –
Prevail true
with !0
and false
with !1
. It saves 2 or 3 characters. –
Miletus It's just to save a byte of data when we do javascript minification.
Consider the anonymous function below:
function (){}
To make the above a self invoking function, we would generally change the above code to:
(function (){}())
Now we added two extra characters: (
and)
, apart from adding ()
at the end of the function, which is necessary to call it. In the process of minification we generally focus on reducing the file size. So we can also write the above function as:
!function (){}()
Both are still self invoking functions and we save a byte as well. Instead of 2 characters (
,)
we just used one character !
.
(function(){})()
? –
Serafina Exclamation mark makes any function always return a boolean.
The final value is the negation of the value returned by the function.
!function bool() { return false; }() // true
!function bool() { return true; }() // false
Omitting !
in the above examples would be a SyntaxError.
function bool() { return true; }() // SyntaxError
However, a better way to achieve this would be:
(function bool() { return true; })() // true
!
changes the way the runtime parses the function. It makes the runtime treat the function as a function expression (and not a declaration). It does this to enable the developer to immediately invoke the function using the ()
syntax. !
will also apply itself (ie negation) to the result of invoking the function expression. –
Untaught ! is a logical NOT operator, it's a boolean operator that will invert something to its opposite.
Although you can bypass the parentheses of the invoked function by using the BANG (!) before the function, it will still invert the return, which might not be what you wanted. As in the case of an IEFE, it would return undefined, which when inverted becomes the boolean true.
Instead, use the closing parenthesis and the BANG (!) if needed.
// I'm going to leave the closing () in all examples as invoking the function with just ! and () takes away from what's happening.
(function(){ return false; }());
=> false
!(function(){ return false; }());
=> true
!!(function(){ return false; }());
=> false
!!!(function(){ return false; }());
=> true
Other Operators that work...
+(function(){ return false; }());
=> 0
-(function(){ return false; }());
=> -0
~(function(){ return false; }());
=> -1
Combined Operators...
+!(function(){ return false; }());
=> 1
-!(function(){ return false; }());
=> -1
!+(function(){ return false; }());
=> true
!-(function(){ return false; }());
=> true
~!(function(){ return false; }());
=> -2
~!!(function(){ return false; }());
=> -1
+~(function(){ return false; }());
+> -1
Its another way of writing IIFE (immediately-invoked function expression).
Its other way of writing -
(function( args ) {})()
same as
!function ( args ) {}();
(function (args) {...})()
syntax and leave that !function
form to minification and obfuscation tools. –
Electron © 2022 - 2024 — McMap. All rights reserved.
!(()=>{})();
or!(function() {})();
– Jennette