Your First JavaScript Function and Return Value
Quick answer
A function is reusable behavior that can receive inputs and return a result. A parameter is a named input in its definition; an argument is the actual value supplied in a call. return ends the current function call and supplies its result.
How it works
In function double(input) { return input * 2; }, double is the function name, input is its parameter, and the braces enclose its body. A call such as double(6) evaluates to 12. The ByteMind documented exercise convention names the entry point solution with one argument; use return, not only console logging, inside it.
function minutes(hours) { return hours * 60;}console.log(minutes(2));console.log(minutes(3));Expected console output (Node.js):
120180The same body calculates a different result for each argument.
function double(input) { return input * 2;}const result = double(6);console.log(result + 1);Expected console output (Node.js):
13The function returns 12, which becomes the value of result; the next expression adds one.
Common mistakes
A function declaration does not automatically execute its body. Calling a function that reaches its end without a return value normally produces undefined; logging inside it is not equivalent to returning a result.
Practice
Write a function that converts days to hours by multiplying the input by 24. Call it with two different arguments.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Your First JavaScript Function and Return Value — Knowledge Check
Review the concept and apply it to a new example.
What is the parameter in function double(input) { return input * 2; }?
What is double(9) for a function returning input * 2?
Which operation supplies the function result to its caller?
When does an ordinary function declaration's body execute?
A function logs 8 and reaches its end without return. What does the call return?