The grouping operator () and functions
In JavaScript, the grouping operator, the parentheses "()" can only contain expressions. Which means I can't do something like this:
(var bar = 7)
JavaScript will get upset and throw a syntax error if I try to input a statement or anything that is not an expression. Simple enough, if the grouping operator only takes expressions then putting a function declaration in it should fail right?
(function foo() {}) // Should throw an error
However, if you run this in your console, it works out fine! It turns out that functions can be expressions as well as a declaration, it simply depends on how you create them. The grouping operator can be great for forcing code to act as an expressions, such using eval on json.
http://kangax.github.io/nfe/ Is a great post that goes into detail on function declaration and expression. I suggest the read.
Written by robotmayo
Related protips
Have a fresh tip? Share with Coderwall community!
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside