+ 1
Can someone explain me how that works?
I am pretty advanced I would say in Java, but new in JS, so you can use Java examples if you want. Here is the code: var a = 5; !function (x) { a = x; } (7); console.log(a); The right answere here was: 7 But I do not understand how that code works, never seen a not operator (!) in front of a function or the parentheses after the function, so I would appreciate if someone can explain me that
2 Answers
+ 2
It's called an immediately invoked function expression (IIFE).
A function declaration is like this
function func(x) {}
func(7);
Which can be shortened to an IIFE.
(function (x) {}) (7);
Or
(function (x) {} (7));
Note how the parentheses are placed.
A more readable way is the way you shared.
!function (x) {} (7);
The exclamation mark signifies that this is a function expression not a function declaration.
0
I cant explain that (was not that your question?)
Maybe you can look in tutorial or google it? Good luck