var functionName = function() {} vs function functionName() {}

The primary distinction between function expressions and function declarations lies in their load time within the code execution process. Function expressions are created and become available only when the interpreter reaches the specific line of code where they are defined. In contrast, function declarations are hoisted to the top of their scope during the initial stages of the code execution, making them accessible and ready for use before any other code is executed. As a result, function expressions are subject to lexical scoping and are not accessible until their definition is reached, while function declarations enjoy the advantage of being available throughout their scope, even before their actual definition in the code.

Function Expression

Function expressions, like any other expressions in JavaScript, are evaluated when the step-by-step execution of the code reaches the specific line where the function expression is defined. As a result, if you attempt to invoke a function expression before it has been reached and defined in the code, an error will occur.

This is because function expressions are not hoisted to the top of their scope during the creation phase like function declarations. Instead, they are created and become available at the exact point in the code where they are defined. Therefore, trying to call a function expression before its definition is encountered during execution will result in a ReferenceError, as the function expression is not yet accessible at that particular moment in the code.

functionExp(); var functionExp = function() { console.log("Example: function expression"); };
HelloWorld.js:2 functionExp(); ^ TypeError: functionExp is not a function at Object.<anonymous> (HelloWorld.js:2:1) at Module._compile (internal/modules/cjs/loader.js:959:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10) at Module.load (internal/modules/cjs/loader.js:815:32) at Function.Module._load (internal/modules/cjs/loader.js:727:14) at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10) at internal/main/run_main_module.js:17:11

If you call a function declaration instead, it'll always work, because no code can be called until all declarations are loaded.

javascript var functionName = function() {} vs function functionName() {}

To avoid such errors, it is essential to ensure that function expressions are defined before they are invoked or called in the code, ensuring proper execution and behavior of the JavaScript program.

Function Declaration

A function declaration is indeed a distinct syntactical category in JavaScript and is not considered a statement or expression. It is a fundamental construct used to define a named function within a given scope.

functionDecl(); function functionDecl() { console.log("Example: function declaration"); }
Example: function declaration

When the code containing a function declaration is executed, the function is processed during the creation phase of the execution context, before any step-by-step code is executed. This process is known as "hoisting." During hoisting, the function is created, and its definition is made available throughout the entire scope in which the declaration appears.

A key feature of function declarations is that they are given a proper name (e.g., functionDecl() in the given example), and this name is associated with the function itself in the scope where the declaration is found. This means that you can call the function using its name anywhere in the same scope, even before the function declaration in the code.


Function declarations play a fundamental role in JavaScript programming, enabling the creation of named functions that can be accessed and reused throughout the program's scope, promoting modularity and maintainability in code design. Understanding the concept of hoisting and how function declarations work is crucial for writing efficient and organized JavaScript code.