Function Expression vs. Function Declaration

Format

// Function Expression
const expression = function () {
    console.log('this is a function expression);
}

// Function Declaration
function declaration () {
    console.log('this is a function declaration);
}

Difference

  1. Name

    • A function expression is a function assigned to a variable (which has a name). So the function itself can be anonymous.

    • However, a function declaration must include a name.

  2. Hoisting

    • Hoisting is allowed only in the function declaration.

        expression(); // error
        declaration(); // this is a function declaration
      
        const expression = function () {
            console.log('this is a function expression');
        }
      
        function declaration () {
            console.log('this is a function declaration');
        }