Lesson 4: Return Values

Learning Objectives

By the end of this lesson, learners will be able to:

  • Understand what the return keyword does in a function
  • Use return to send data back from a function
  • Differentiate between return and console.log()
  • Store and reuse return values in variables
  • Build functions that produce useful results

What Is a Return Value?

In JavaScript, a function can give back a result using the return keyword. This value can then be:

  • stored in a variable,
  • used in calculations,
  • passed into another function, and more.

Think of return as the “output” of a function — like how a calculator returns a result after a calculation.


Syntax

function functionName(parameters) {
  // code
  return value;
}
  • return: ends the function and sends a value back
  • The value can be a number, string, boolean, object, etc.

Example 1: Returning a Greeting

function getGreeting(name) {
  return "Hello, " + name + "!";
}

let message = getGreeting("Emma");
console.log(message); // Output: Hello, Emma!
  • The function returns a string
  • The result is stored in message and used later

Difference: return vs. console.log()

Purposereturnconsole.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

Example:

function add(a, b) {
  return a + b;
}

console.log(add(3, 4)); // Output: 7
  • return a + b; sends the result back
  • console.log(...) displays the result

Example 2: Reuse the Returned Value

function 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.


Functions Stop After 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.


Functions Without 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 console
  • But it returns nothing, so output is undefined

Summary

ConceptDescription
returnEnds the function and sends back a result
Use casesStore value in a variable, use in calculations, etc.
console.log()Only used to display something — it does not return
After returnCode does not execute after a return statement