Understanding Function Statements and Function Expressions in JavaScript

Rehmat Sayany
2 min readJul 30, 2023

--

Introduction

JavaScript offers different ways to define and use functions, and two of the most common approaches are function statements (also known as function declarations) and function expressions. In this blog post, we will dive into the similarities and differences between these two techniques.

Function Statements (Function Declarations)

A function statement, or function declaration, is the traditional way of defining a function in JavaScript. This type of function is named and has its body enclosed between curly braces {}.

function sayHello() {
console.log('Hello, world!');
}

One key characteristic of function statements is that they are hoisted, which means they are moved to the top of the scope by the JavaScript interpreter. Therefore, you can call a function declared this way before its definition in the code:

sayHello(); // This works!
function sayHello() {
console.log('Hello, world!');
}

Function Expressions

On the other hand, a function expression is a function that is assigned to a variable. The function can be named, or it can be anonymous. Here’s an example:

let sayGoodbye = function() {
console.log('Goodbye, world!');
}

In this case, sayGoodbye is not a function, but a variable that holds a function. Because of this, function expressions are not hoisted like function statements are. If you try to call a function expression before its definition, you will get an error:

sayGoodbye(); // This will throw an error!
let sayGoodbye = function() {
console.log('Goodbye, world!');

Function expressions are often used when a function is only needed in one place or when a function needs to be passed as an argument to another function (a key concept in functional programming).

Named vs. Anonymous Function Expressions

As we mentioned earlier, function expressions can be named or anonymous. An anonymous function expression looks like this:

let sayGoodbye = function() {
console.log('Goodbye, world!');
}

A named function expression provides a name inside the function definition:

let sayGoodbye = function goodbye() {
console.log('Goodbye, world!');
}

The name of a named function expression is only in scope within the function itself, which can be useful for recursion or debugging.

Conclusion

In summary, while function statements and function expressions in JavaScript may seem similar, they have distinct differences, especially regarding hoisting. Understanding these differences is crucial for writing effective JavaScript code.

Remember, function statements are hoisted and can be invoked before their declaration. On the contrary, function expressions (either named or anonymous) are not hoisted, so they must be defined before they are used.

Make use of both these concepts in your JavaScript projects, as they can provide greater flexibility and control in different situations. Happy coding!

--

--

Rehmat Sayany
Rehmat Sayany

Written by Rehmat Sayany

Full Stack developer @westwing passionate about NodeJS, TypeScript, React JS and AWS.

No responses yet