What is difference between var functionName = function vs function functionName

What is the difference between: var functionName = function() {} and function functionName() {} in Javascript?

There are two ways of declaring functions in JS. Which are called function expression and a function declaration.

The first example is a function declaration:

The second example is a function expression:

Let’s see the difference between them:

What is difference between var functionName function vs function functionName JavaScript

function expression example code

The function expression defined when that line is reached.

The function expression is often called an “anonymous function”, because it does not have to have a name,

function declaration example code

Function declaration is defined as soon as its surrounding function or script is executed (due to hoisting).

Note: Hoisting is the JavaScript interpreter’s action of moving all variable and function declarations to the top of the current scope.

Performance test

Function declarations are faster (source: stackoverflow.com).

Function declarations and expression performance

Do comment if you have any suggestion or questions on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.