Languages
[Edit]
EN

JavaScript - function declaration vs function expression

0 points
Created by:
Reilly-Collier
860

In this article, we would like to show you the difference between function declaration and function expression in JavaScript.

Quick solution:

1. function myFunction() {} - is a function declaration,

2. var myFunction = function() {} - is a function expression.

 

Practical example

Edit

In this section, we present the difference between function declaration and function expression in a practical example.

1. function declaration

Edit

In this example, myFunction() is a function declaration and due to hoisting, it is defined whenever the surrounding function or script is executed.

2 function expression

Edit

In this example, myFunction() is a function expression, that is defined only after reaching this line.

Example 1:

Example 2:

 

See also

Edit
  1. JavaScript - difference between let and var

References

Edit
  1. Function declaration - JavaScript | MDN
  2. Function expression - JavaScript | MDN

Alternative titles

  1. JavaScript - difference between var functionName = function() {} and function functionName() {}
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join