
The Difference Between Function Expression and Function Declaration in JavaScript
Function expressions and declarations have two major differences. One is that for function expressions, you are assigning the function to a variable. In a function declaration, you don't:
// Function expression
// Will throw a TypeError
console.log(func());
var func = function() { return 5; }
// Function declaration
// Will run without a problem, because it is hoisted
console.log(func());
function func() { return 5; }
Copied to clipboard!
The biggest difference however is that expressions are not hoisted. If you call the function before it was initialized, you will get a type error:

Whichever you choose to use throughout your code, make sure you stay consistent to avoid unexpected behaviors.

Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.

Resources:
π More Webtips
Master the Art of Frontend
Unlimited access to hundred of tutorials
Access to exclusive interactive lessons
Remove ads to learn without distractions