JavaScript Functions Explained: Function Declarations vs Function Expressions
What Are Functions?
A function is a reusable block of code that performs a specific task.
Instead of writing the same logic multiple times, we place that logic inside a function and call it whenever we need it.
Example Without a Function
let result1 = 5 + 3;
let result2 = 10 + 7;
let result3 = 20 + 4;
We repeat the same logic multiple times.
Example With a Function
function add(a, b) {
return a + b;
}
console.log(add(5, 3));
console.log(add(10, 7));
console.log(add(20, 4));
Now the logic is written once and reused multiple times.
Functions help us:
Reuse code
Make programs easier to read
Organize logic better
2. Function Declaration
A function declaration defines a function using the function keyword and a name.
Syntax
function functionName(parameters) {
// code to execute
}
Example
function greet(name) {
return "Hello " + name;
}
console.log(greet("Rahul"));
Here:
greetis the function namenameis the parameterThe function returns a greeting message
3. Function Expression
A function expression stores a function inside a variable.
Syntax
let variableName = function(parameters) {
// code
};
Example
let greet = function(name) {
return "Hello " + name;
};
console.log(greet("Rahul"));
Here the function does not have to be named because it is stored inside the variable greet.
4. Declaration vs Expression (Side by Side)
Function Declaration
function add(a, b) {
return a + b;
}
Function Expression
let add = function(a, b) {
return a + b;
};
Both perform the same task, but they behave differently in some situations.
5. Basic Idea of Hoisting
In JavaScript, function declarations are hoisted.
This means they can be called before they appear in the code.
Works with Function Declaration
console.log(add(2, 3));
function add(a, b) {
return a + b;
}
This works because JavaScript moves the function declaration to the top during execution.
Does NOT Work with Function Expression
console.log(add(2, 3));
let add = function(a, b) {
return a + b;
};
This causes an error because the variable add is not initialized yet.
6. Key Differences
| Feature | Function Declaration | Function Expression |
|---|---|---|
| Syntax | Uses function name() |
Stored in a variable |
| Hoisting | Fully hoisted | Not usable before definition |
| Use case | General reusable functions | Often used inside variables, callbacks |
7. When to Use Each
Use Function Declaration
When defining main reusable functions
When you want the function available everywhere in the file
Use Function Expression
When assigning functions to variables
When using functions as arguments
When working with callbacks
8. Practice Assignment
Try the following exercise.
1. Function Declaration
Write a function that multiplies two numbers.
function multiply(a, b) {
return a * b;
}
console.log(multiply(4, 5));
2. Function Expression
Write the same logic using a function expression.
let multiply = function(a, b) {
return a * b;
};
console.log(multiply(4, 5));
3. Experiment with Hoisting
Try calling the functions before defining them and observe what happens.
