Cohort2026ChaiCodeJavaScriptwebdev

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

In programming, we hate repeating ourselves. If you have a specific task—like calculating a discount or greeting a user—you don't want to write that same…

Kenil Patel
Kenil PatelFull Stack Engineer
Function Declaration vs. Function Expression: What’s the Difference?

In programming, we hate repeating ourselves. If you have a specific task—like calculating a discount or greeting a user—you don't want to write that same code 50 times. You want a Function.

Functions are essentially reusable blocks of code that perform a specific task. You define them once, and you can "call" them whenever you need them. In JavaScript, there are two primary ways to create these blocks: Function Declarations and Function Expressions.

While they might look similar, they behave quite differently under the hood. Let’s break it down.


1. Function Declaration

A Function Declaration is the traditional way of defining a function. It starts with the function keyword, followed by the name you want to give it.

Syntax:

JavaScript

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

console.log(addNumbers(5, 10)); // Output: 15

Key Characteristic: It’s like a "named recipe" in a cookbook. It exists independently and has a clear name that JavaScript recognizes immediately.


2. Function Expression

A Function Expression happens when you create a function and assign it to a variable. Because the function is stored in a variable, it doesn't technically need its own name (this is called an anonymous function).

Syntax:

JavaScript

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

console.log(addNumbers(5, 10)); // Output: 15

Key Characteristic: The function is treated like a value (like a string or a number) that is stored inside a box (the variable).


The Big Difference: Hoisting

This is the most important part. In JavaScript, Hoisting is a behavior where the interpreter moves declarations to the top of the current scope before executing the code.

With Declarations:

You can call a Function Declaration before you define it in your code. JavaScript "lifts" the declaration to the top.

JavaScript

greet(); // Output: "Hello!"

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

With Expressions:

You cannot call a Function Expression before it is defined. Because it is stored in a variable (like const or let), JavaScript treats it like any other variable—it doesn't exist until the code reaches that line.

JavaScript

greet(); //  Error: Cannot access 'greet' before initialization

const greet = function() {
  console.log("Hello!");
};

Think of it like this: A Function Declaration is like a famous celebrity everyone knows before they enter the room. A Function Expression is like a guest who has to show their ID at the door before they are allowed to speak.


Summary Comparison

Feature

Function Declaration

Function Expression

Syntax

function name() { ... }

const name = function() { ... }

Hoisted?

Yes (Can call it before definition)

No (Must define it before calling)

Usage

Great for global, reusable logic.

Great for local logic or passing into other functions.

Flexibility

Rigid name.

Can be anonymous or used as a callback.


When to use each?

  • Use Declarations when you want a function to be available throughout your entire script and you prefer the traditional, easy-to-read structure.

  • Use Expressions when you want to keep your functions local to a specific block of code, or when you are using modern JavaScript patterns like Arrow Functions (which are a type of expression).


Assignment: Test Your Knowledge

Try this in your code editor to see the difference firsthand:

  1. The Declaration: Write a function declaration named multiply that takes two numbers and returns their product.

  2. The Expression: Write the exact same logic but store it in a variable named multiplyExpr.

  3. The Test: Call both functions at the very bottom of your script.

  4. The "Hoisting" Challenge: Try moving the function calls to the top of the file (before the definitions). Observe which one still works and which one crashes your program!


Conclusion

Both declarations and expressions have their place in a developer's toolkit. Declarations offer the convenience of hoisting, while expressions offer more control and fit perfectly into modern, modular coding styles.

Mastering the difference is a huge step toward becoming a professional JavaScript developer!