There are two different ways to create a function in JavaScript: function expressions and function declarations.

function funcDeclaration() {
  return 'A function declaration';

const funcExpression = function() {
  return 'A function expression';

The main difference is that function declarations are hoisted to the top of the scope so they load and have access to local variables before any other scope has been run.

Because function expressions are not hoisted, they work better in three instances:

  • closures
  • arguments to other functions
  • Immediately Invoked Function Expressions (IIFE)

Want to improve your JavaScript? I have a list of recommended JavaScript books.