JavaScript function declarations & function expressions

If you are learning Javascript you may have noticed that one of the first things one learns about, is function declarations. I know I did. Today I want to do a small review on function declarations and function expressions.


Take a look at these two functions, notice the difference? It may seem as if both have the same name ‘add’ but only the first function is actually called ‘add’. The second function is assigned to the variable ‘add’.

Function declarations

Every function declaration starts with the keyword function and must have a name in order to be able to refer to the function. Functions define as function declarations are hoisted, these means that the function is available anywhere in the current scope. One may invoke the function before defining it and it will run without throwing any errors (assuming there is no bugs in our function). Function declarations are statements.

Function expressions

Function expressions look similar to function declarations but these are assigned to a variable. As the name suggests, a function expression, is an expression. Assigning to a variable, means that the function is a value expression and must be terminated with a semicolon. Invoking the function before defining it will result in an error. The function does not get hoisted only the variable name. The variable is hoisted with the value of undefined until the function is assigned to it.

This was a quick review on function declarations and function expressions but hope it helps.