Javascript: !function (x) {} | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Javascript: !function (x) {}

What does "!" mean here, in front of "function"? var a = 5 !function (x) { a = x; }(7);

24th Aug 2020, 1:54 PM
Prof. Dr. Zoltán Vass
4 Answers
+ 7
Use cases: - Code Golfing. - JS minifiers. It's used for the sole purpose of reducing the number of characters by 1. Otherwise the classics (function())() or (function()()) are preferred because of better readability.
25th Aug 2020, 4:02 PM
Kevin ★
+ 6
In this snippet, the unary operator(!) is replacing the parens that normally surrounds the function to create an Immediately-invoked Function Expression (IIFE). You may see other unary operators (+, ~, -, void) used in the same. JavaScript Immediately-invoked Function Expressions (IIFE) https://flaviocopes.com/javascript-iife/ JavaScript Unary Operators: Simple and Useful https://scotch.io/tutorials/javascript-unary-operators-simple-and-useful https://code.sololearn.com/cIsuovG1mCyh/#node
24th Aug 2020, 2:43 PM
ODLNT
ODLNT - avatar
+ 5
https://stackoverflow.com/questions/9267289/what-does-function-in-javascript-mean Similar like calling anonymous function but I didn't checked that we could use ! instead of ( until now!! (function(){ })()
24th Aug 2020, 2:23 PM
Abhay
Abhay - avatar
+ 4
It makes no sense until you assign the function call expression to something ... b = !function(x){ ... }(7); Somehow the function was executed, *perhaps* because JS sees that the function's return value was needed as operand for logical NOT operator. But Idk what value was returned as I see nothing was being returned. Log test in console said that !<function-call-expression> evaluates to `true` BTW ...
24th Aug 2020, 2:27 PM
Ipang