Declaring functions


I always thought you'd declare a function by doing something like

function functionName() {}

But in this lesson it's done as

var functionName = function() {}

Is there any difference? Do both work?


The first is a function statement, the second is a function expression. The statement version is created before the rest of the code runs:

f();  // ok
g();  //  doesn't exist

function f() {}
var g = function() {};



Is there any practical difference, as in any reason to ever use one over the other?


You might want to create a function without assigning it to a variable


Okay, thanks for the help! :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.