Loading Depth Study
Preparing your workspace
A function is a reusable block of code that can receive input, perform work, and optionally return a result.
Functions help break large programs into small understandable pieces. A function can accept parameters and return a value. Function declarations are useful when defining named reusable behavior. Function expressions store functions in variables, while arrow functions provide a shorter syntax and have different this behavior. A well-designed function usually performs one clear task and avoids unnecessary side effects.
A learning platform can have a function that calculates a student's course completion percentage from completed and total lessons.
function calculateProgress(completed, total) {
if (total === 0) return 0;
return Math.round((completed / total) * 100);
}
const progress = calculateProgress(8, 10);
console.log(progress);The console prints 80.