Start learning free

LESSON 33 OF 348 · FUNCTIONS I

Calling a Function Twice 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 greetCoder() {
	console.log("Hello!");
	console.log("Let's code!");
}
	
greetCoder();
greetCoder();

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 Calling A Function in TypeScript, explained

Problem overview

The quiz shows a function with two print statements and two calls to it:

function greetCoder() {
    console.log("Hello!");
    console.log("Let's code!");
}

greetCoder();
greetCoder();

Four outputs are offered. Option A prints both lines twice, in order. Option B prints Hello! twice and then Let's code! twice. Option C prints both lines once. Option D prints both lines three times. The answer is stored in a variable named answer as a letter.

Approach: trace the program line by line

The reliable method is to run the program in your head and write down what each executed line prints. Two rules cover this quiz. A declaration runs nothing, and a call runs the whole body from top to bottom every time.

Step by step

  1. The declaration. The first four lines define greetCoder. TypeScript records the function, and the two console.log statements inside do not run.
  2. The first call. greetCoder(); runs the body in order: it prints Hello!, then Let's code!. When the last statement finishes, execution returns to the line after the call.
  3. The second call. The next greetCoder(); runs the same body again from the top: Hello!, then Let's code!.
  4. Put the output together. Each call finishes completely before the next begins, so the pairs stay together:
    Hello!, Let's code!, Hello!, Let's code!.
  5. Match an option. That is option A. Option B would require the two calls to interleave, which never happens. Option C forgets the second call. Option D counts a third run that the declaration doesn't cause.

What happens during a call

When TypeScript reaches greetCoder();, it pauses the line it is on, jumps into the function and runs the body from its first statement. When the body ends, execution comes back and continues with the line right after the call. That is why the two calls produce two complete, separate runs. It also explains why a function is useful: the two print statements are written once and used twice, so changing the greeting later would mean editing one place instead of two.

The solution in TypeScript

        let answer: string = "A";

        /* Explanation:
In the first 4 lines of code we only define the function, so no "console.log()" statement will actually get executed.

Then we call the function for the first time, so all the statements inside the function body will get executed from top to bottom.
So, the first function call will print "Hello!", then it will print "Let's code!" on two separate lines, and then this function call ends.
The output so far is:
Hello!
Let's code!

Then we call the function for the second time, and again all the statements inside the function body will get executed from top to bottom.
So the second function call will again print "Hello!", then it will print "Let's code!" on two separate lines, and then the function call ends.
So the final output is:
Hello!
Let's code!
Hello!
Let's code!
*/

Notes on the code:

  • The variable is declared with let and typed string. The annotation is only a check by the compiler and is erased from the JavaScript output.
  • The answer is a string, so "A" needs its quotes.
  • The comment is not code and is ignored by the program.

Running the quiz's code gives exactly this output:

Hello!
Let's code!
Hello!
Let's code!

Edge cases and common mistakes

  • Counting the declaration as a call. Defining the function doesn't run it. Only the two lines with greetCoder(); do. That is the reason option D, with three runs, is wrong.
  • Merging the two calls. A call is not a loop over the statements. Each call completes its whole body before the next starts, so the output never looks like option B.
  • Assuming a repeated call is skipped. A function does not remember that it already ran. Every call runs the body again.
  • Missing the apostrophe. The string "Let's code!" uses double quotes, so the apostrophe inside it is just a character and needs no escape.

Testing your understanding

  • With one call, the output would be the two lines once (option C).
  • With three calls, the pair would appear three times (option D).
  • If the second call were removed and the function declared but never called, nothing would print.
Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free