Skip to main content

Command Palette

Search for a command to run...

Function Declaration vs Function Expression: What’s the Difference?

The Power of Reusability: A Practical Guide to Writing Efficient and Scalable JavaScript Functions

Updated
4 min readView as Markdown
Function Declaration vs Function Expression: What’s the Difference?

What functions are and why we need them

What are Functions?

A function is a reusable block of code that performs a specific task. Functions are executed when they are called or invoked. Functions are the fundamental in all programming languages. Think of a function like a machine. You give it some input, it processes it and then gives you the output. For example : A function to add two numbers. Instead of writing the same logic repeatedly, we can create a function once and reuse it whenever needed.

Example:

function greet() {
    console.log("Hello, welcome to JavaScript!");
}

greet();

Output:

Hello, welcome to JavaScript!

Here:

  • function greet() defines the function

  • greet() this calls the function and tells the function to execute.

Why Do We Need Functions?

We need Functions Because Functions helps us in many ways:

Reusability

we can reuse the same function multiple times in a program.

Cleaner Code

Code becomes easier to read and maintain.

Better Organization

Functions allow us to divide large programs into smaller logical parts.

Example :

function add(a, b) {
  return a + b;
}

console.log(add(5, 3));
console.log(add(10, 7));

Output:

8

17

  • The same function works with different values.

Function declaration syntax

A function declaration defines a function using the function keyword followed by a name .

Syntax:

function functionName(parameters) {
  // code
}

Example :

function addNumbers(a, b) {
  return a + b;
}

let result = addNumbers(4, 6);

console.log(result);  // output : 10

Here:

  • addNumbers this is a function name

  • a, b this are parameters of a functions

  • return sends the result back


Function expression syntax

A function expression stores a function inside a variable. Simply assign a function to a variable.

Syntax :

let variableName = function(parameters) {
  // code
};

Example :

let addNumbers = function(a, b) {
  return a + b;
};

console.log(addNumbers(5, 3)); // output : 8

Here, the function is assign to a variable called addNumbers .


Key difference between declaration and expression


Basic idea of hoisting

Hoisting is a JavaScript behavior where some declarations are moved to the top of the scope during execution. Functions declarations can be used before they appear in the code.

Example :

sayHello();

function sayHello() {
  console.log("Hello!");
}

Output:

Hello!

This works because function declarations are hoisted.

Function Expression and Hoisting

Functions Expression behave differently. Function expression are not hoisted but function declaration are hoisted.

Example :

sayHello();

let sayHello = function() {
  console.log("Hello!");
};

This will throw an error. Because the variable is created first, but the function assignment happens later. So the function cannot be used before its definition.


When to use each type

Use Function Declaration When :

  • The function is core logic of the program

  • You want to call the function before its definition

  • You want cleaner and simpler code

Use Function Expressions When :

  • You want to assign function to variables

  • You are using callbacks.

  • You want functions inside the expressions or conditions.

Both approaches are commonly used in JavaScript.


Wrapping up 📌

Thanks for reading till the end 🙌
I hope this article helped you understand the topic in a simple, practical, and beginner-friendly way.

My goal is to break down complex tech concepts into clear, real-world explanations, especially for learners who are just starting out or feeling overwhelmed.

If you found this useful, feel free to bookmark, share, or leave a comment - it really helps and keeps me motivated to write more.

You can connect with me here:

Let’s learn together and grow step by step 🚀