Understanding function declarations vs. function expressions in JavaScript

Eric Nelson
3 min readApr 19, 2023

--

When you first start learning JavaScript, an important concept you will quickly run in to is the difference between function declarations and function expressions and when they should be used. While they may look very similar there are a few key differences that must be considered, as it could make or break your code.

First lets define the terms

Function declarations are a way to define named functions in JavaScript. They start with the “function” keyword, followed by the name of the function, a set of parentheses that contain any parameters the function accepts, and a set of curly braces that contain the code for the function.

function exampleFunc(x) {
console.log(x);
}

It can then be called by its name.

exampleFunc(example)

Function expressions are another way to define functions in JavaScript. They start with the function keyword, followed by any parameters the function accepts, and a set of curly braces that contain the code for the function. However, function expressions don't have a name, and they're usually assigned to a variable.

const exampleFunc = function(x) {
console.log(x);
}

It can then be called using its variable name if assigned one.

exampleFunc(example)

You may notice that when you call these example functions, the calls are identical.

So what are the key differences?

Hoisting: The biggest fundamental difference between the two methods is how they are hoisted. Function declarations are hoisted to the top of their scope, meaning you can call them before they are defined in your code. Function expressions, on the other hand, are not hoisted and cannot be called until after they are defined.

Example 1:

const result = exampleFunc(1, 2)

function exampleFunc(x, y) {
return x + y;
}

Example 2:

const result = exampleFunc(1, 2)

const exampleFunc = function(x, y) {
return x + y;
}

Example 1 would return 3 to result while Example 2 would return the error “exampleFunc is not defined”.

Naming: Function declarations require a name, while function expressions can be anonymous or have a name. Having a named function can come in hand when you plan on reusing a function multiple times in your code. it can also make your code easier to read and understand. On the other hand, there are times when an anonymous function is more appropriate or concise.

Variables: Function declarations create a variable in the current scope that is bound to the function, while function expressions create a variable that is bound to the function only where the expression is evaluated.

Clarity: Function declarations may be easier to read and understand, since the name of the function is clear and the entire function is defined in one statement. Function expressions may be harder to read and understand, since they can be split across multiple lines and may not have a clear name.

In Conclusion,

Understanding the differences between Function declarations and Function expressions is essential for writing effective and optimized JavaScript code. Function declarations are hoisted to the top of their scope, require a name, and create a variable in the current scope that’s bound to the function. Function expressions, on the other hand, are not hoisted, can be anonymous or have a name, and create a variable that’s assigned to the function. Both approaches have their advantages and disadvantages, and choosing the right one depends on the specific needs of your code. By understanding the subtle differences of function declarations and function expressions, you can write better code that’s easier to read, maintain, and debug.

Resources and/or Further Reading

https://chat.openai.com/

--

--