This flow shows declaring a function, optionally calling it, then declaring a class, creating an object, and using its methods or properties.
Execution Sample
Typescript
function greet(name: string): string {
return`Hello, ${name}!`;
}
class Person {
constructor(public name: string) {}
greet() { return`Hi, I am ${this.name}`; }
}
Declares a function greet that returns a greeting string, and a class Person with a name property and a greet method.
Execution Table
Step
Action
Evaluation
Result
1
Declare function greet
Function greet(name) created
Function stored in memory
2
Declare class Person
Class Person created with constructor and greet method
Class stored in memory
3
Call greet('Alice')
Execute greet with name='Alice'
Returns 'Hello, Alice!'
4
Create new Person('Bob')
Call constructor with name='Bob'
Object with name='Bob' created
5
Call person.greet()
Execute greet method on Person object
Returns 'Hi, I am Bob'
6
End
No more actions
Program ends
💡 All declarations done and function/class usage demonstrated, program ends.
Variable Tracker
Variable
Start
After Step 3
After Step 4
After Step 5
Final
greet
undefined
function greet
function greet
function greet
function greet
Person
undefined
class Person
class Person
class Person
class Person
person
undefined
undefined
Person object {name:'Bob'}
Person object {name:'Bob'}
Person object {name:'Bob'}
greet('Alice') result
undefined
'Hello, Alice!'
'Hello, Alice!'
'Hello, Alice!'
'Hello, Alice!'
person.greet() result
undefined
undefined
undefined
'Hi, I am Bob'
'Hi, I am Bob'
Key Moments - 3 Insights
Why do we declare the function before calling it?
Because in the execution_table at step 1, the function greet is created and stored in memory, so it can be called later at step 3. If we call it before declaration, it would cause an error.
What happens when we create a new Person object?
At step 4, the constructor runs with the given name, creating an object with that name property. This is shown in the variable_tracker where person changes from undefined to an object.
How does the greet method access the name property?
In step 5, person.greet() uses 'this.name' to access the object's name property, returning a string with the name. This shows how methods use 'this' to refer to the current object.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of calling greet('Alice') at step 3?
A'Hi, I am Alice'
Bundefined
C'Hello, Alice!'
DError
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step is the Person object created?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column for object creation in the execution_table.
If we change the name in new Person('Bob') to 'Eve', what changes in variable_tracker after step 4?
Aperson becomes undefined
Bperson becomes {name: 'Eve'}
Cgreet function changes
DNo change
💡 Hint
Check the 'person' variable values in variable_tracker after step 4.
Concept Snapshot
Declaring functions:
function name(params): returnType { body }
Declaring classes:
class Name { constructor(...) {...} methods... }
Functions run when called.
Classes create objects with properties and methods.
Use 'this' inside class methods to access object data.
Full Transcript
This visual execution shows how to declare functions and classes in TypeScript. First, the function greet is declared and stored in memory. Then the class Person is declared with a constructor and a greet method. When greet('Alice') is called, it returns a greeting string. Creating a new Person object runs the constructor to set the name property. Calling person.greet() returns a greeting using the object's name. Variables change as functions and objects are created and used. This step-by-step trace helps understand how declarations and calls work in TypeScript.
Practice
(1/5)
1. What is the main purpose of a function in TypeScript?
easy
A. To create a new type of variable
B. To store data without any behavior
C. To perform a specific task and optionally return a result
D. To define a constant value
Solution
Step 1: Understand what a function does
A function is a block of code designed to perform a specific task.
Step 2: Recognize the role of return values
Functions can return results after performing their task, making them reusable.
Final Answer:
To perform a specific task and optionally return a result -> Option C
Quick Check:
Function purpose = perform task + return result [OK]
Hint: Functions do tasks and can give back results [OK]
Common Mistakes:
Confusing functions with variables
Thinking functions only store data
Believing functions cannot return values
2. Which of the following is the correct way to declare a function named greet that takes a string name and returns a string in TypeScript?
easy
A. function greet(name: string): string { return `Hello, ${name}`; }
B. function greet(name) { return 'Hello, ' + name; }
C. function greet(name: string) { console.log('Hello, ' + name); }
D. function greet(): string { return name; }
Solution
Step 1: Check function parameter and return types
function greet(name: string): string { return `Hello, ${name}`; } correctly declares the parameter name as a string and the return type as string.
Step 2: Verify function body returns a string
function greet(name: string): string { return `Hello, ${name}`; } returns a template string greeting the name, matching the return type.
Final Answer:
function greet(name: string): string { return `Hello, ${name}`; } -> Option A
Quick Check:
Correct parameter and return types = function greet(name: string): string { return `Hello, ${name}`; } [OK]
Hint: Specify parameter and return types explicitly [OK]
Common Mistakes:
Omitting parameter types in TypeScript
Not returning a value when return type is declared
Using console.log instead of return
3. What will be the output of the following TypeScript code?
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
return `Hi, I am ${this.name}`;
}
}
const p = new Person('Anna');
console.log(p.greet());
medium
A. Hi, I am Anna
B. Hi, I am undefined
C. Error: Property 'name' does not exist
D. Hi, I am
Solution
Step 1: Understand class construction
The Person class sets this.name to the passed argument 'Anna' in the constructor.
Step 2: Analyze the greet method output
The greet method returns a string including this.name, which is 'Anna'.
Final Answer:
Hi, I am Anna -> Option A
Quick Check:
Class property set correctly = 'Hi, I am Anna' [OK]
Hint: Constructor sets properties; methods use them [OK]
Common Mistakes:
Forgetting to assign constructor parameter to this.name
Expecting undefined because of missing this
Confusing method return with console.log output
4. Identify the error in this TypeScript class declaration:
class Car {
model: string;
constructor(model) {
this.model = model;
}
display() {
console.log('Model: ' + this.model);
}
}
medium
A. Property 'model' should be private
B. Missing return type for display method
C. Constructor should not have parameters
D. Missing type annotation for constructor parameter 'model'
Solution
Step 1: Check constructor parameter types
In TypeScript, constructor parameters should have type annotations; here, model lacks a type.
Step 2: Verify other parts
The display method does not require a return type annotation since it returns void implicitly.
Final Answer:
Missing type annotation for constructor parameter 'model' -> Option D
Quick Check:
Constructor parameters need types = Missing type annotation for constructor parameter 'model' [OK]
Hint: Always type constructor parameters in TypeScript [OK]
Common Mistakes:
Omitting parameter types in constructors
Thinking methods must always declare return types
Assuming properties must be private by default
5. You want to create a TypeScript class Calculator with a method add that takes two numbers and returns their sum. Which code correctly implements this with proper types and usage?
hard
A. class Calculator {
add(a, b) {
return a + b;
}
}
const calc = Calculator();
console.log(calc.add(5, 3));
B. class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add(5, 3));
C. class Calculator {
add(a: number, b: number) {
console.log(a + b);
}
}
const calc = new Calculator();
calc.add(5, 3);
D. class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add('5', '3'));
Solution
Step 1: Check method declaration and types
class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add(5, 3)); declares add with parameters typed as numbers and return type number, which is correct.
Step 2: Verify object creation and method call
class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add(5, 3)); creates an instance with new Calculator() and calls add(5, 3), which returns 8.
Final Answer:
Correct class with typed method and proper usage -> Option B
Quick Check:
Typed method + new instance + correct call = class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
const calc = new Calculator();
console.log(calc.add(5, 3)); [OK]
Hint: Use 'new' to create class instances and type parameters [OK]
Common Mistakes:
Calling class without 'new'
Omitting parameter types
Passing wrong argument types (like strings instead of numbers)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.