Returning Values

JavaScript ECMAScript 2024 · ✓ verified by execution on

Sending Data Back

In the previous lesson, we saw how functions take input through parameters. But a program where functions can only receive data and print to the console is very limited. To build complex software, functions must be able to compute a result and give it back to whoever called them.

In JavaScript, you do this using the return statement. The return statement ends function execution immediately and specifies the exact value to be returned to the function caller. This allows you to capture the result in a variable and use it later in your code.

javascript
function square(x) {
  return x * x;
}
let result = square(4);
console.log(result);
Output
16

Visualizing the Return

Watch the tracer step through the function line-by-line, calculate the value, hit the return statement, and pass the data back out to the main execution flow:

javascript · visualize
function multiply(a, b) {
  let result = a * b;
  return result;
}

let value = multiply(3, 4);
console.log(value);
12

The Immediate Stop

A crucial behavior of the return statement is that it acts as an immediate exit hatch for the function. When JavaScript executes a return statement, it immediately stops executing any further code in that function body. It completely ignores anything that comes after it in the same code block.

javascript
function earlyReturn() {
  return 'done';
  console.log('This will never run');
}
console.log(earlyReturn());
Output
done

This behavior is incredibly useful for a pattern known as “early return”, which allows you to check for invalid conditions at the top of your function and exit before running the main logic.

javascript
function checkPositive(n) {
  if (n <= 0) {
    return 'Not positive';
  }
  return 'Positive number!';
}
console.log(checkPositive(-2));
console.log(checkPositive(5));
Output
Not positive
Positive number!

Implicit Undefined Returns

One of the most common beginner questions in JavaScript is: “Why does my function return undefined?”

The answer is simple: If a return statement has no expression, or if a function simply reaches the end of its closing brace {} without ever hitting a return statement at all, JavaScript automatically returns the special value undefined. It does not return null, nor does it throw an error. It just returns undefined.

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

function emptyReturn() {
  return;
}
console.log(emptyReturn());
Output
undefined

Let’s see what happens when we omit the return keyword altogether:

javascript
function noReturn() {
  let x = 5;
}
console.log(noReturn());
Output
undefined

The Return Machine

Here is a visual representation of how return passes data back out to the global scope:

Function Scope return 'done' let result =
A return statement ends the function and hands its value back to the caller, where it is captured in a variable.

Syntax and Strictness

The return statement can only be used within function bodies. Trying to use return in the global scope of a script will result in a hard crash (SyntaxError).

Finally, be very careful with line breaks. Because of JavaScript’s Automatic Semicolon Insertion (ASI), placing a newline directly after the return keyword will cause JavaScript to insert an invisible semicolon right after return, making it evaluate to undefined and ignoring the expression on the next line.

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

function testASI() {
  return
  'hello';
}
console.log(testASI());
Output
undefined

Check yourself

If you call a function with fewer arguments than there are parameters, what value is assigned to the missing parameters?

Reveal answer

undefined — Missing arguments are assigned the value `undefined` in JavaScript.

Which of the following statements about JavaScript function parameters is true?

Reveal answer

They are passed by value — Parameters are essentially passed to functions by value. Re-assigning a parameter inside the function doesn't change the original variable.

What is 'hoisting' in the context of function declarations?

Reveal answer

The function declaration is moved to the top of its scope, allowing it to be called before it appears in the code. — Function declarations can be hoisted, meaning they appear below the call in the code but still work.

What happens if you pass more arguments than a function has parameters?

Reveal answer

The extra arguments are ignored by the function parameters. — Extra arguments are ignored by the named parameters (though they can still be accessed via the `arguments` object).

Challenges

Challenge 1 +15 XP

Write a function named `sayWelcome` that prints 'Welcome' to the console. We will call it for you.

javascript
  • Test 1 — expects "Welcome\n"
Need a hint? (−25% XP)

Use the `function` keyword, followed by the name `sayWelcome`, parentheses `()`, and curly braces `{}`.

Show solution (0 XP)
function sayWelcome() {
  console.log('Welcome');
}

// Do not modify below
sayWelcome();

Challenge 2 +20 XP

Write a function named `printDouble` that takes one parameter `n` and prints `n * 2` to the console. We will call it for you.

javascript
  • Test 1 — expects "10\n"
Need a hint? (−25% XP)

Put `n` inside the parentheses of your function declaration.

Show solution (0 XP)
function printDouble(n) {
  console.log(n * 2);
}

// Do not modify below
printDouble(5);