Thursday, June 19, 2014

JavaScript function: expressions vs declarations

A detailed explanation of nuanced differences in JavaScript syntax and semantics

Named function expressions demystified:
"In a nutshell, named function expressions are useful for one thing only — descriptive function names in debuggers and profilers. Well, there is also a possibility of using function names for recursion"

function foo(){} // declaration, since it's part of a Program
var bar = function foo(){}; // expression, since it's part of an AssignmentExpression

new function bar(){}; // expression, since it's part of a NewExpression

(function(){
  function bar(){} // declaration, since it's part of a FunctionBody
})();

"function declarations are parsed and evaluated before any other expressions are."



No comments: