Last Updated: February 25, 2016
·
964
· robotmayo

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.