Learning Objectives
- Understand the differences between function declarations, function expressions, and arrow functions.
- Write reusable functions using parameters and return values.
- Understand the concept of scope.
- Use default parameters.
- Understand the concept of callback functions.
1. Function Declaration
// Function declaration
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("John Doe")); // "Hello, John Doe!"
- Hoisting: A function declaration can be called before its position in the code.
console.log(sayHi()); // Works (hoisting)
function sayHi() {
return "Hi!";
}
2. Function Expression
// Assign a function to a variable
const greet = function(name) {
return `Hello, ${name}!`;
};
console.log(greet("John Doe")); // "Hello, John Doe!"
- No hoisting: Calling it before the declaration causes an error.
3. Arrow Function (ES6+)
// Basic form
const greet = (name) => {
return `Hello, ${name}!`;
};
// Parentheses can be omitted when there is a single parameter
const double = n => {
return n * 2;
};
// Braces and return can be omitted when the function body is a single line
const triple = n => n * 3;
const add = (a, b) => a + b;
console.log(double(5)); // 10
console.log(triple(5)); // 15
console.log(add(3, 4)); // 7
4. Parameters and Return Values
// Multiple parameters
function calculateBMI(weight, height) {
const bmi = weight / (height * height);
return bmi.toFixed(1);
}
const myBMI = calculateBMI(70, 1.75);
console.log(`BMI: ${myBMI}`); // "BMI: 22.9"
// A function with no return value (void)
function logMessage(message) {
console.log(`[LOG] ${message}`);
// No return → returns undefined
}
5. Default Parameters (ES6+)
// Set default values for parameters
function greet(name = "Guest", lang = "en") {
if (lang === "en") {
return `Hello, ${name}!`;
} else {
return `Hi, ${name}!`;
}
}
console.log(greet()); // "Hello, Guest!"
console.log(greet("John Doe")); // "Hello, John Doe!"
console.log(greet("Sam", "casual")); // "Hi, Sam!"
6. Scope
// Global scope
const globalVar = "global variable";
function outer() {
const outerVar = "outer variable";
function inner() {
const innerVar = "inner variable";
console.log(globalVar); // accessible
console.log(outerVar); // accessible (closure)
console.log(innerVar); // accessible
}
inner();
// console.log(innerVar); // Error! Cannot access outside inner's scope
}
outer();
7. Callback Functions
A callback function is a function that is passed as an argument to another function and is called later.
// Callback function example
function doTask(task, callback) {
console.log(`Task started: ${task}`);
// Call the callback after the task completes
callback(task);
}
function onComplete(task) {
console.log(`Task completed: ${task}`);
}
doTask("Save file", onComplete);
// "Task started: Save file"
// "Task completed: Save file"
// Inline callback with an arrow function
doTask("Send data", (task) => {
console.log(`✅ ${task} succeeded!`);
});
Common Callback Patterns
// setTimeout: runs the callback after n milliseconds
setTimeout(() => {
console.log("Runs after 1 second");
}, 1000);
// Callbacks in array methods (covered in detail in Lesson 04)
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2); // [2, 4, 6, 8, 10]
Practice Files
- View the example
- Check the assignment
- Assignment solution