Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these two function declarations in JavaScript? [duplicate]

Tags:

javascript

Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}

In JavaScript we can say:

function a() {};

Or we could say

var a = function() {};

Can anyone explain to me how exactly these differ, which, if any, is more preferable, and under what circumstances would one use each?

Any links or external reading would also be much appreciated.

like image 598
James Wiseman Avatar asked Jan 31 '11 19:01

James Wiseman


People also ask

What's the difference between a function declaration and function expression?

A function expression is very similar to and has almost the same syntax as a function declaration (see function statement for details). The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions.

What is a function declaration in JavaScript?

The function declaration (function statement) defines a function with the specified parameters. You can also define functions using the Function constructor and a function expression.

What are the types of functions in JavaScript?

There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.


1 Answers

One is a function declaration and one is a function expression.

http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

like image 124
Jason LeBrun Avatar answered Nov 15 '22 01:11

Jason LeBrun