Declaring functions


#1

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?


#2

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() {};

#3

Thanks!

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


#4

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


#5

Okay, thanks for the help! :slight_smile:


#6

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