Function Expression vs Function Declaration in Javascript

Mastering JavaScript Functions: Declarations, Expressions, and the Magic of Hoisting
Imagine you have a favorite recipe for chocolate chip cookies. Every time you want cookies, you don't invent the recipe from scratch—you just pull out your recipe card and follow the instructions.
In JavaScript, Functions are your recipe cards. They are reusable blocks of code designed to perform a specific task. Instead of writing the same lines of code over and over again, you write it once inside a function and simply "call" it whenever you need it.
Let's explore the two most common ways to write functions in JavaScript, how they differ, and a quirky JavaScript behavior known as "hoisting."
Function Declarations: The Traditional Way
A Function Declaration is the standard, most recognizable way to create a function. You start with the function keyword, give it a name, define its parameters, and write the code block.
Let's write a simple function that adds two numbers:
// Defining the function declaration
function addNumbers(a, b) {
return a + b;
}
// Calling the function
let result = addNumbers(5, 10);
console.log(result); // Output: 15
(Diagram Idea: Insert a simple execution flow diagram here showing the inputs 5 and 10 going into an addNumbers box, and the output 15 coming out.)
Function declarations are clean, readable, and easy to spot in your codebase because they always start with that function keyword.
Function Expressions: Functions as Values
In JavaScript, functions are treated as "first-class citizens." This means you can treat a function just like any other value (like a string or a number). You can store a function inside a variable!
When you create a function and assign it to a variable, it is called a Function Expression.
Let's rewrite our addition logic using an expression:
// Defining the function expression
const addNumbersExpression = function(a, b) {
return a + b;
};
// Calling the function
let sum = addNumbersExpression(7, 3);
console.log(sum); // Output: 10
Notice that the function itself doesn't have a name after the function keyword (this is called an anonymous function). Instead, the variable name addNumbersExpression becomes the name we use to call it.
The Magic of Hoisting (and the Key Difference)
At a glance, declarations and expressions look like two ways of doing the exact same thing. But there is one massive difference in how JavaScript handles them behind the scenes, and it comes down to a concept called Hoisting.
Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope before the code actually executes.
Let's see this in action:
Hoisting with Function Declarations
With function declarations, you can call the function before you actually write the code for it! JavaScript scans the file, finds the declaration, and "hoists" it to the top.
// Calling it BEFORE it is defined - This WORKS!
console.log(greet("Alice")); // Output: Hello, Alice!
function greet(name) {
return `Hello, ${name}!`;
}
Hoisting with Function Expressions
Function expressions are not hoisted in the same way. If you try to call a function expression before you define it, JavaScript will throw an error.
// Calling it BEFORE it is defined - This FAILS!
console.log(sayHi("Bob")); // Error: Cannot access 'sayHi' before initialization
const sayHi = function(name) {
return `Hi, ${name}!`;
};
Summary Table: Declaration vs. Expression
Here is a quick reference guide to help you remember the differences:
Feature | Function Declaration | Function Expression |
Syntax |
|
|
Hoisting | Fully hoisted. Can be called before definition. | Not hoisted. Cannot be called before definition. |
Use Case | Best for global or utility functions needed anywhere in the file. | Best for localized logic, passing as arguments (callbacks), or keeping scope clean. |
When to Use Which?
Use Function Declarations when you want the freedom to call the function anywhere in your file, or when you are creating core utility functions that multiple parts of your script will rely on.
Use Function Expressions when you want to tightly control where a function is available, preventing it from being called before it's explicitly defined. This is great for keeping your code execution predictable.
Try It Yourself! (Assignment)
Let's put this into practice with a quick challenge:
Write a function declaration called
multiplyDecthat takes two numbers and returns their product.Write a function expression called
multiplyExpthat does the exact same thing.Call both functions at the very bottom of your file and
console.logthe results.The Hoisting Test: Move the
console.logcalls to the very top of your file, above the function definitions. Run the code. Observe which one works and which one throws an error!




