JavaScript functions: declarations, calls, parameters, and returns
Declare a JavaScript function with function name(parameter) { ... } and call it with name(argument). The body runs at the call. Use return to send a result back to the caller.
Declare a function and call it
function double(value) {
return value * 2;
}
const result = double(4);
console.log(result); // 8
In double(4), value is the parameter in the definition and 4 is the argument. The same function can run with different inputs. Keep the calculation separate from display: return hands the value back, while console.log() or textContent displays it. See the function guide.
Omitted arguments and return values
function greet(name = "Visitor") {
return `Hello, ${name}.`;
}
greet("Min"); // "Hello, Min."
greet(); // "Hello, Visitor."
greet(undefined); // "Hello, Visitor."
greet(null); // "Hello, null."
A default parameter applies when an argument is omitted or is undefined, but not when it is null. Validate other values separately. return ends the current function. An ordinary function with no return value produces undefined. Keep the expression or an opening parenthesis on the same line as return to avoid automatic semicolon insertion.
Compare a declaration with a function expression
console.log(add(2, 3)); // 5
function add(a, b) {
return a + b;
}
const subtract = function (a, b) {
return a - b;
};
console.log(subtract(5, 2)); // 3
A function declaration is set up before other statements run in its scope, so the earlier call to add works. This is called hoisting. A const function expression becomes callable after its initializer runs. Calling subtract first would access the binding too early.
Separate input, calculation, and output
Save the UTF-8 code as function-demo.html and open it. totalMinutes() calculates, while update() handles input and display. The script follows the elements it uses.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Calculate total time with a function</title>
</head>
<body>
<h1>Calculate time for repeated tasks</h1>
<label for="minutes">Minutes per session</label><input id="minutes" value="15">
<label for="sessions">Sessions</label><input id="sessions" value="3">
<button id="calculate" type="button">Calculate</button>
<button id="restore" type="button">Restore defaults</button>
<p id="output"></p>
<script>
function totalMinutes(minutes, count = 1) {
return minutes * count;
}
const minutesInput = document.getElementById("minutes");
const sessionsInput = document.getElementById("sessions");
const output = document.getElementById("output");
function update() {
const minutes = Number(minutesInput.value);
const count = Number(sessionsInput.value);
if (minutesInput.value.trim() === "" || sessionsInput.value.trim() === "" ||
!Number.isFinite(minutes) || minutes <= 0 ||
!Number.isInteger(count) || count < 1) {
output.textContent = "Enter a positive duration and an integer count of at least 1.";
return;
}
const result = totalMinutes(minutes, count);
output.textContent = `Total: ${result} minutes`;
}
document.getElementById("calculate").addEventListener("click", update);
document.getElementById("restore").addEventListener("click", () => {
minutesInput.value = "15";
sessionsInput.value = "3";
update();
});
update();
</script>
</body>
</html>
The initial display is Total: 45 minutes for 15 × 3. Change the count to 4 and click Calculate to get 60. A count of 1.5, a blank field, or a duration of 0 is rejected. Restore defaults returns to 15 and 3. Calling totalMinutes(15) directly uses count 1. An empty input field is not an omitted argument. The early return prevents an invalid calculation.
Pass a function as a callback
// Register update to run on click
button.addEventListener("click", update);
// This would call update now and pass its return value
// button.addEventListener("click", update());
Register update as a click callback to run it later. update() runs immediately and supplies its return value. The example calls it immediately once to render the initial output. Extra arguments are available through rest parameters and, in ordinary functions, through arguments.
Add a variable number of session durations
Run the next code in the Console. The rest parameter ...durations collects the supplied minute values in an array. A function can have one rest parameter and it comes last.
{
function totalSessionMinutes(...durations) {
let total = 0;
for (const minutes of durations) {
total += minutes;
}
return total;
}
console.log(totalSessionMinutes(15, 25, 20)); // 60
console.log(totalSessionMinutes()); // 0
const savedSessions = [10, 35];
console.log(totalSessionMinutes(...savedSessions)); // 45
}
The outputs are 60, 0, and 45. With no arguments the array is empty. The declaration collects values, while the call with ...savedSessions expands an array into individual arguments. This function expects numbers. Convert and validate form strings first.




