Start learning free

LESSON 48 OF 289 · FUNCTIONS I

Print Instead of Return in TypeScript: Fixing getGreeting

Practice Read the full explanation

We've written a program and expected it to print Hey, Andy but when we run it something else is printed.

Fix our code so that it works and prints what we want.

STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · FUNCTIONS I

    Buggy Code Print Instead Of Return in TypeScript, explained

    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.log shows a value in the output. It does not give anything back to the code that called the function.
    • return hands 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

    1. Keep the bottom two lines. They already print whatever getGreeting gives them, so the printing is the caller's job.
    2. Remove the printing from the function. If getGreeting printed as well, the greeting would appear twice or be mixed with undefined.
    3. Return the string instead. Replace console.log(...) with return followed 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 then return "Hey, " + name; prints the greeting twice.
    • Returning the wrong thing. return console.log("Hey, " + name); prints the greeting inside the function and then returns what console.log gives back, which is undefined. 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 get Hey,Andy.

    A quick comparison

    StatementShows text in the outputGives a value to the caller
    console.log(x)yesno, the call is undefined
    return xnoyes

    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.

    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free