Jump to navigation

Function Expression/Declaration

Function Declaration

A Function Declaration defines a named function variable without requiring variable assignment. Function Declarations occur as standalone constructs and are hoisted to the top of their scope.

function test() {
  return true;

Function Expression

A Function Expression defines a function as a part of a larger expression syntax (typically a variable assignment ). Functions defined via Functions Expressions can be anonymous or named. Function Expressions must not start with “function”.

// anonymous
const a = () => {
  return true;
// named - useful if they need to reference themselves (e.g. for recursive calls)
var a = function test() {
  return 3;
// self-invoking
(function test() {

Which one should I use?

// Function Declaration
function test(text) {return text};

// Function Expression
var test = function(text) {return text};
Crockford recommends to use function expression because it makes it clear that foo is a variable containing a function value. Well, personally, I prefer to use Declaration unless there is a reason for Expression. Emile Bergeron - Stackoverflow