11 Expressions

2010-05-15

Expressions is snippet of code that as a whole returns a value. I guess this is the only true difference between them in Javascript. Expressions may not be start of a Program, that's why you'll always see parenthesis around anonymous functions:

Code: (Ecma)
(function(){
... do stuff that would polute global ...
})();

It's because otherwise the program would fail.

There can be a lot of confusion in Ecmascript between the difference of Expression and Statement, especially with regard to function. A function is a statement if it is an unassigned named function:

Code: (Ecma)
function foo(){}

Anything else is considered an expression:
Code: (Ecma)
var x = function(){};
var x = function foo(){};
function(){}

Note that only function statements are parsed before execution (and hoisted).