Function Declaration vs Function Expression: What’s the Difference?

What functions are and why we need them?
Functions are the basic building blocks of any programming language. Functions are the reusable set of instructions that we can use in our program.
There are various benefits of using functions:
It provides reusability of code.
It helps in breaking down our big code into smaller blocks.
It helps in improving the code readability.
Function declaration syntax
//FUNCTION DECLARATION
function greet() {
console.log('Hello World');
}
Function expression syntax
//FUNCTION EXPRESSION
const greet = function () {
console.log('Hello World');
}
Key differences between declaration and expression
Function declaration can be used before declaration whereas Function expression cannot be used before declaration.
Function declaration need a function name while declaration whereas Function expression do not have function name and stored in a variable.
Basic idea of hoisting (very high level)
Function Declaration is Hoisted and can be used before declaration because all function declarations are completely hoisted in memory phase.
Function expression are stored inside a variable and variables do not carry their value in hoisting, that's why we cannot use the function before declaration.
When to use each type
We can use Function declaration when we need our function to be hoisted and do not want to store inside a variable.
We can use Function expression when we do not want hoisting and want our function to be stored inside a variable.





