What execution flow is
A program starts on its first line and runs one line at a time, from top to bottom. That order is its execution flow. Function declarations are the one place where the flow looks different: the lines in a function body are not run when TypeScript reads them. They run later, each time the function is called.
Take this program:
console.log("It's a beautiful day.");
function sayHello() {
console.log("Hello coders!");
console.log("Welcome to the lesson!");
}
console.log("Students are joining.");
sayHello();
console.log("We'll learn about functions.");
It prints:
It's a beautiful day.
Students are joining.
Hello coders!
Welcome to the lesson!
We'll learn about functions.
Tracing it
console.log("It's a beautiful day.")runs and prints the first line.- The declaration of
sayHellois next. TypeScript notes that the function exists. The two print statements inside it are part of the body, so they are not executed now. - The closing brace ends the definition. Everything after it belongs to the main program again.
console.log("Students are joining.")prints the second line.sayHello();is a call. Execution jumps into the body, printsHello coders!andWelcome to the lesson!, then returns to the line after the call.- The last
console.logprints the final line.
The key point is that the order of the definitions in the file doesn't decide the output. The order of the calls does. The function sits in the middle of the file, but its messages appear after the line that follows it, because the call is placed there.
Braces and indentation
TypeScript finds the end of a function body by its curly braces. Indentation, the spaces in front of the lines inside the body, doesn't change how the program runs. It is there for readers: it shows at a glance which lines belong to the function. Keep the body indented consistently, and keep the closing brace in line with the function keyword.
The lesson's exercise
The task is to produce the letters A to E, each on its own line, in order. The solution uses a class named Solution, with two methods. A method is a function that belongs to a class.
- Write
sayHello. It printsB,CandD, oneconsole.logper letter. It takes no parameters and is annotated: void, because it only prints. - Write
runbelow it. It printsA, then callssayHello, then printsE. The call tosayHellosits between the two prints, which putsB,CandDbetweenAandE. - Call the other method through
this. Inside a class, one method calls another withthis.sayHello(). Writing justsayHello()would look for a separate function with that name, which doesn't exist here.
class Solution {
sayHello(): void {
console.log("B");
console.log("C");
console.log("D");
}
run(): void {
console.log("A");
this.sayHello();
console.log("E");
}
}
Calling new Solution().run() prints:
A
B
C
D
E
Although sayHello is written first, run decides the order. If run called this.sayHello() before printing A, the output would start with B.
Why a class here
The lesson's examples used a plain function, while the exercise wraps the code in a class. The execution rule is the same in both. The class declaration only describes the two methods and prints nothing. Output appears only when something calls run, and run then calls sayHello through this. Seen this way, the exercise is the earlier trace again, with the main program replaced by the body of run.
Gotchas
- Defining is not running. Nothing prints until
runis called, and without the call tothis.sayHello()inside it, onlyAandEwould appear. - Methods versus functions. Inside the class there is no
functionkeyword. The method name is followed directly by parentheses and the body. - Forgetting
this. Inside a method, a baresayHello()does not refer to the method of the same class, and the compiler reports that it can't find the name. The JavaScript it writes then stops withReferenceError: sayHello is not definedwhenrunis called.
Key takeaways
- Code runs top to bottom, one line at a time.
- A function body is skipped over when defined and runs on each call.
- The order of the calls decides the order of the output.
- Indentation helps readers; braces tell TypeScript where the body ends.