Why can't the increment increment operator (++) be used for IIFE?
I am learning javascript and am amazed at the syntax IIFE
.
I realized that to write, IIFE
we need to make the function as an expression and then call it.
We can do this as an expression by wrapping a function between ()
. Or the prefix of the key functions of speech by using +
, -
, ~
, !
.
Now for the problem, when I prefix with ++
, I get an error in the console.
Code:
++function(){console.log("hello")}();
Mistake:
Uncaught ReferenceError: Invalid left-hand side expression in prefix operation
Why can't I use ++
? ++
is a unary operator, and I thought it would make the interpreter think of an anonymous function as a function expression, like +
, -
etc., did.
Where am I going wrong?
source to share
Unable to assign
As the error message says, function () ... is not a valid left expression, i.e. cannot be assigned.
+, -, ~ ,! will cause the expression to be evaluated. On the other hand, the increment (++) operator will cause the expression to not only be evaluated but also modified, which is not allowed for this expression.
source to share