Functions in Javascript (Declaration, Expression, Arrow)

There are three common ways of using functions and each has its own name — “Function Declaration” and “Function Expression”. There is also “Arrow Function”, which has the shortest syntax.

The syntactical differences between them are like so:

Function Declaration is declared as a separate statement, in the main code flow.

Function Expressions are created inside another expression or statement, such as a variable declaration. The function below is treated like a value which gets assigned into a variable — that variable can then be called as a function.

Arrow function offers a shorter syntax like so:

If we have only one argument, then parentheses can be omitted, making that even shorter:

If there are no arguments, parentheses should be empty (but they should be present):

If there is more than one line inside the code block encapsulated by the pair of { } , we would need to use the “return” keyword, like so :

Now, let’s discuss about the the more subtle difference between the Function Declaration and Function Expression. It has to do with the execution time of the function.

A Function Expression is created when the execution reaches it and is usable from then on.

Once the execution flow passes to the end side of the assignment,

at that point, the function is created and can be used thereafter (assigned, called etc).

Function Declarations are different.

A Function Declaration is usable in the whole script/code block.

In other words, when JavaScript prepares to run the script or a code block, it first looks for Function Declarations in it and creates the functions. Much like var that is hoisted; declaring a variable anywhere in the code is equivalent to declaring it at the top.

And after all of the Function Declarations are processed, the execution moves on.

As a result, a function declared as a Function Declaration can be called earlier than it is defined.

For example, this works:

The Function Declaration sayHi is created when JavaScript is preparing to start the script and is visible everywhere in it.

If it were a Function Expression, then it wouldn’t work:

Function Expressions are created when the execution reaches them. That would happen only in the line marked with (*) .

Another post will cover specifically about Arrow Functions, but the gist about Arrow Functions is:

  • Do not have their own this (inherit it from its nearest object-bound parent, read more here)
  • Do not have arguments
  • Can’t be called with new
  • Do not have super

--

--

Frontend developer with master’s degrees in Computer Science and Politics. Passionate about animals, software accessibility and UI/UX design.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maya Novarini

Frontend developer with master’s degrees in Computer Science and Politics. Passionate about animals, software accessibility and UI/UX design.