Start learning free

LESSON 45 OF 348 · FUNCTIONS I

Forgotten Return in TypeScript: Quiz Explained

Quiz Read the full explanation

Quiz: What would this code produce if we were to copy-paste it in a code editor and run it?

function getDouble(n: number) {
	n * 2;
}

let result = getDouble(10);
console.log(result);

Important Note:

Do not use an actual code editor to get the answer! It would defy the whole purpose of the quiz!

Pick your answer

Next lesson

TYPESCRIPT · FUNCTIONS I

Quiz Forgotten Return in TypeScript, explained

The quiz

The quiz shows a function and asks what the program prints. The function is:

function getDouble(n: number) {
    n * 2;
}

let result = getDouble(10);
console.log(result);

The options are: A, it prints 20; B, it prints undefined; C, it produces errors; D, it prints 10. The answer goes into a variable named answer, holding the letter of your choice as a string.

Working it out

Trace the program in the order it runs.

  1. The call. getDouble(10) stores 10 in n and runs the body.
  2. The body. The only statement is n * 2;. It computes 20 and then throws the result away: nothing stores it, prints it or returns it.
  3. The end of the function. The body finishes without reaching a return statement. A function that ends this way hands undefined to its caller.
  4. The assignment. result receives that undefined.
  5. The print. console.log(result) prints undefined.

That matches option B. Each other option rests on a common wrong idea:

  • A, 20. This assumes the function sends back the last value it computed. Some languages do that, but TypeScript and JavaScript don't: a value comes back only through return.
  • C, errors. A line that only computes a value is legal. It is an expression statement, and the code has no return to be missing, because the function declares no return type. The compiler accepts the program, and it runs normally.
  • D, 10. This mixes up the argument with the result. n is 10 inside the function, but nothing sends it back.

The solution in TypeScript

The lesson asks for the answer as a string held in a variable annotated with string:

        let answer: string = "B";

        /* Explanation:
Notice that inside the function we only compute n * 2 but do nothing with the result afterwards, we do not return it from the function.

When there is no return statement inside a function, TypeScript/JS does not throw any errors,
but returns the value undefined by default from that function.

So getDouble(10) function call will return undefined and this undefined value will then be assigned to variable result.
And so console.log(result) will print undefined to the console.
*/

The quoted letter is a string, so "B" in double quotes is the value to assign. The annotation : string documents that, and the compiler would reject, say, let answer: string = 2;.

What the compiler knows

TypeScript sees that getDouble has no return and infers the return type void, which means "no useful value". At run time the annotation is gone and the call evaluates to undefined.

Writing the return type yourself makes the bug visible. If the function were declared as function getDouble(n: number): number with the same body, the compiler would report that a function whose declared type is not void or any must return a value. That is one of the best reasons to annotate return types.

The fix

To make the function behave as its name suggests, return the value:

function getDouble(n: number): number {
    return n * 2;
}

let result = getDouble(10);
console.log(result); // 20

Now the call evaluates to 20, which is option A for this corrected version.

Common mistakes

  • Running the code instead of tracing it. The quiz is about reading code, and tracing by hand builds the skill you need for debugging.
  • Answering with the wrong form. The variable should hold just the letter, for example "B", not the option text.
  • Expecting an error for a missing return. Without a declared return type, nothing warns you. The result is silently undefined.
  • Confusing printing with returning. Adding console.log(n * 2) in the function would print 20 but would still return undefined.

Key takeaways

  • A function gives a value back only through return.
  • A function that finishes without return evaluates to undefined.
  • An expression statement such as n * 2; computes a value and discards it.
  • Declaring the return type lets the compiler catch a forgotten return.
Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free