What the code is supposed to do
The function getGreeting should build a greeting for name and give it back to its caller. The caller, at the bottom, stores the result in greeting and prints it. The program should print Hey, Andy exactly once. As written, it prints Hey, Andy and then undefined.
function getGreeting(name: string): string {
console.log("Hey, " + name);
}
// Do not change the code below:
const greeting: string = getGreeting("Andy");
console.log(greeting);
What goes wrong
Two different things are easy to confuse here:
console.logshows a value in the output. It does not give anything back to the code that called the function.returnhands a value back to the caller. It shows nothing by itself.
Follow the program line by line. The call getGreeting("Andy") runs the body, and the body prints Hey, Andy. That is the first line of output. The body then ends without a return, so the call evaluates to undefined, and that value is stored in greeting. Finally console.log(greeting) prints it: the second line, undefined.
Hey, Andy
undefined
The declared return type string does not change this. The compiler reports error TS2355 on the header, because a function declared to return a string must return a value, but types are erased when the code becomes JavaScript, so at run time the call simply produces undefined.
Fixing it step by step
- Keep the bottom two lines. They already print whatever
getGreetinggives them, so the printing is the caller's job. - Remove the printing from the function. If
getGreetingprinted as well, the greeting would appear twice or be mixed withundefined. - Return the string instead. Replace
console.log(...)withreturnfollowed by the same expression,"Hey, " + name.
The fixed code in TypeScript
function getGreeting(name: string): string {
return "Hey, " + name;
}
// Do not change the code below:
const greeting: string = getGreeting("Andy");
console.log(greeting);
Now the call evaluates to the string "Hey, Andy", greeting holds it, and the final console.log prints it once.
Why returning is the better design
A function that returns its result can be used in many ways: print it, store it, compare it, or pass it to another function. A function that only prints can do just one thing. In this lesson the caller is the one that decides to print, which is why the printing belongs outside the function.
Common mistakes
- Keeping both. Writing
console.log("Hey, " + name);and thenreturn "Hey, " + name;prints the greeting twice. - Returning the wrong thing.
return console.log("Hey, " + name);prints the greeting inside the function and then returns whatconsole.loggives back, which isundefined. The output is the same as the bug. - Forgetting the space. The text is
"Hey, "with a comma and a space before the closing quote. Without the space you would getHey,Andy.
A quick comparison
| Statement | Shows text in the output | Gives a value to the caller |
|---|---|---|
console.log(x) | yes | no, the call is undefined |
return x | no | yes |
Concatenation with + is what builds the greeting. The text "Hey, " and the value of name are joined into one new string, and that new string is what return sends out. Nothing needs to be stored in an extra variable first, although const text = "Hey, " + name; return text; would work the same way.
Reading the symptom
The wrong output tells you where to look. A stray undefined after a correct-looking line means a function printed its result and returned nothing, and a caller then printed the missing result. Whenever you see undefined in the output, ask which call produced it and whether that function has a return.
Checking your fix
Run the code in the editor above. The output must be exactly one line: Hey, Andy. Any undefined line means a function in the program is still being used for its result without returning one.
