JavaScript Functions: Function Declaration vs Function Expression
What Are Functions?
A function is a reusable block of code that performs a specific task.
Instead of writing the same code multiple times, you can place it inside a function and call it whenever you need it.
Think of a function like a machine:
Input ā Function ā Output
Example: adding two numbers.
function add(a, b) {
return a + b;
}
console.log(add(3, 5));
Output:
8
Here:
addis the function nameaandbare parametersThe function returns the result
Function Declaration
A function declaration defines a function using the function keyword followed by a name.
Syntax
function functionName(parameters) {
// code to execute
}
Example
function greet(name) {
console.log("Hello " + name);
}
greet("Rahul");
Output:
Hello Rahul
In this example:
greetis the function"Rahul"is passed as an argument
Function Expression
A function expression stores a function inside a variable.
Syntax
const functionName = function(parameters) {
// code
};
Example
const greet = function(name) {
console.log("Hello " + name);
};
greet("Riya");
Output:
Hello Riya
Here:
The function does not need a name
It is stored inside the variable
greet
Declaration vs Expression (Side-by-Side)
Function Declaration
function add(a, b) {
return a + b;
}
Function Expression
const add = function(a, b) {
return a + b;
};
Both do the same job, but they behave differently in certain situations.
Understanding Hoisting (Simple Explanation)
Hoisting means JavaScript moves certain declarations to the top of the code before execution.
This affects function declarations and function expressions differently.
Function Declaration and Hoisting
You can call the function before it is defined.
Example:
sayHello();
function sayHello() {
console.log("Hello!");
}
This works because function declarations are hoisted.
Function Expression and Hoisting
Function expressions cannot be used before they are defined.
Example:
sayHello();
const sayHello = function() {
console.log("Hello!");
};
This will cause an error because the variable exists but the function is not assigned yet.
Comparison Table
| Feature | Function Declaration | Function Expression |
|---|---|---|
| Syntax | function name(){} |
const name = function(){} |
| Hoisting | ā Fully hoisted | ā Not hoisted |
| Use before definition | ā Yes | ā No |
| Stored in variable | ā No | ā Yes |
| Flexibility | Basic | More flexible |
When Should You Use Each?
Use Function Declarations When:
You want a simple reusable function
You want to call the function before its definition
Writing basic program logic
Example:
function calculateTotal(price, tax) {
return price + tax;
}
Use Function Expressions When:
You want to store functions in variables
You want to pass functions as arguments
Working with callbacks or advanced patterns
Example:
const calculateTotal = function(price, tax) {
return price + tax;
};
Simple Execution Flow of a Function Call
Program Starts
ā
Function is Defined
ā
Function is Called
ā
Code Inside Function Runs
ā
Result is Returned
Example:
function multiply(a, b) {
return a * b;
}
console.log(multiply(4, 5));
Output:
20
Practice Assignment š§
Try implementing the following yourself.
1ļøā£ Write a Function Declaration
Create a function that multiplies two numbers.
function multiply(a, b) {
return a * b;
}
console.log(multiply(4, 3));
2ļøā£ Write the Same Logic Using Function Expression
const multiply = function(a, b) {
return a * b;
};
console.log(multiply(4, 3));
3ļøā£ Try Calling Functions Before Definition
Experiment with this:
console.log(add(2, 3));
function add(a, b) {
return a + b;
}
Now try this with a function expression:
console.log(add(2, 3));
const add = function(a, b) {
return a + b;
};
Observe the difference and understand how hoisting works.