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.
- The call.
getDouble(10)stores10innand runs the body. - The body. The only statement is
n * 2;. It computes20and then throws the result away: nothing stores it, prints it or returns it. - The end of the function. The body finishes without reaching a
returnstatement. A function that ends this way handsundefinedto its caller. - The assignment.
resultreceives thatundefined. - The print.
console.log(result)printsundefined.
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 throughreturn. - C, errors. A line that only computes a value is legal. It is an expression statement, and the code has no
returnto 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.nis10inside 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 print20but would still returnundefined.
Key takeaways
- A function gives a value back only through
return. - A function that finishes without
returnevaluates toundefined. - An expression statement such as
n * 2;computes a value and discards it. - Declaring the return type lets the compiler catch a forgotten
return.