By the end of this lesson, learners will be able to:
return
keyword does in a functionreturn
to send data back from a functionreturn
and console.log()
In JavaScript, a function can give back a result using the return
keyword. This value can then be:
Think of
return
as the “output” of a function — like how a calculator returns a result after a calculation.
function functionName(parameters) {
// code
return value;
}
return
: ends the function and sends a value backfunction getGreeting(name) {
return "Hello, " + name + "!";
}
let message = getGreeting("Emma");
console.log(message); // Output: Hello, Emma!
message
and used laterreturn
vs. console.log()
Purpose | return | console.log() |
---|---|---|
Sends value back | ✅ Yes | ❌ No |
Displays output | ❌ No (unless used in console.log ) | ✅ Yes (for debugging) |
Use in logic | ✅ Can be used in expressions | ❌ Just prints to the console |
function add(a, b) {
return a + b;
}
console.log(add(3, 4)); // Output: 7
return a + b;
sends the result backconsole.log(...)
displays the resultfunction double(x) {
return x * 2;
}
let result = double(5);
console.log("Double is:", result); // Output: Double is: 10
✅ You can now reuse the result for further logic.
return
Any code written after the return
statement won’t run:
function test() {
return "Done";
console.log("This won't run");
}
🧠 Always put return
at the end if you want other code to run first.
return
If a function doesn’t explicitly return something, it returns undefined
by default:
function greet(name) {
console.log("Hello, " + name);
}
let output = greet("Liam");
console.log(output); // Output: Hello, Liam → undefined
greet()
prints to the consoleoutput
is undefined
Concept | Description |
---|---|
return | Ends the function and sends back a result |
Use cases | Store value in a variable, use in calculations, etc. |
console.log() | Only used to display something — it does not return |
After return | Code does not execute after a return statement |