Bird
Raised Fist0
Typescriptprogramming~10 mins

Class property declarations in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Class property declarations
Start Class
↓
Declare Properties
↓
Constructor (optional)
↓
Create Instance
↓
Access/Modify Properties
↓
End
This flow shows how a class declares properties, optionally initializes them in a constructor, then creates instances to access or modify those properties.
Execution Sample
Typescript
class Person {
  name: string;
  age = 30;

  constructor(name: string) {
    this.name = name;
  }
}
Defines a Person class with a name property set in the constructor and an age property with a default value.
Execution Table
StepActionProperty 'name'Property 'age'Notes
1Class Person declared with propertiesundefined30age has default value 30, name not set yet
2Create instance p = new Person('Alice')undefined30Before constructor runs
3Constructor runs: this.name = 'Alice'Alice30name property set to 'Alice'
4Instance p readyAlice30Properties initialized
5Modify p.age = 35Alice35age property changed to 35
6EndAlice35Instance properties final state
💡 Instance properties set and modified, execution ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
p.nameundefinedundefinedAliceAliceAlice
p.age3030303535
Key Moments - 3 Insights
Why is 'name' undefined before the constructor runs?
Because 'name' is declared but not given a default value, it is set only inside the constructor (see execution_table step 2 and 3).
Why does 'age' have a value before the constructor?
'age' has a default value of 30 declared directly in the class, so it exists on the instance immediately (see execution_table step 1 and 2).
Can we change property values after creating the instance?
Yes, properties like 'age' can be changed anytime after instance creation (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'p.name' at step 2?
Aundefined
B'Alice'
C30
Dnull
💡 Hint
Check the 'Property name' column at step 2 in the execution_table.
At which step does 'p.age' change from 30 to 35?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Property age' column changes in the execution_table.
If we remove the default value for 'age', what would 'p.age' be at step 2?
A30
Bundefined
Cnull
DError
💡 Hint
Default values set property before constructor; without it, property is undefined (see variable_tracker).
Concept Snapshot
Class property declarations in TypeScript:
- Declare properties inside class with optional default values.
- Properties without defaults are undefined until set (usually in constructor).
- Constructor can initialize properties.
- Properties can be accessed and changed on instances anytime.
- Example: class Person { name: string; age = 30; constructor(name: string) { this.name = name; } }
Full Transcript
This visual trace shows how class properties are declared and initialized in TypeScript. The class Person has two properties: 'name' declared without a default and 'age' with a default of 30. When a new Person instance is created with 'Alice' as the name, the constructor sets the 'name' property. The 'age' property starts with 30 because of its default. Later, 'age' can be changed. The execution table tracks these changes step-by-step, and the variable tracker shows property values at each stage. Key moments clarify why properties have certain values before and after construction. The quiz tests understanding of property initialization and modification. This helps beginners see how class properties work in real code.

Practice

(1/5)
1. What is the purpose of declaring properties inside a TypeScript class?
easy
A. To import external modules into the class
B. To define functions that the class can use
C. To create new classes inside the current class
D. To store data related to each object created from the class

Solution

  1. Step 1: Understand what class properties represent

    Class properties hold data or information specific to each object created from the class.
  2. Step 2: Differentiate properties from methods and other elements

    Functions inside classes are called methods, not properties. Properties are for storing values.
  3. Final Answer:

    To store data related to each object created from the class -> Option D
  4. Quick Check:

    Class properties = store object data [OK]
Hint: Properties hold object data, methods hold actions [OK]
Common Mistakes:
  • Confusing properties with methods
  • Thinking properties create new classes
  • Believing properties import modules
2. Which of the following is the correct way to declare a class property with a type and default value in TypeScript?
easy
A. class Car { speed: = 0 number; }
B. class Car { speed: number = 0; }
C. class Car { number speed = 0; }
D. class Car { speed = number 0; }

Solution

  1. Step 1: Recall TypeScript property syntax

    Properties are declared as name: type = defaultValue;.
  2. Step 2: Check each option for correct syntax

    class Car { speed: number = 0; } matches the correct syntax: speed: number = 0;. Others have incorrect order or missing punctuation.
  3. Final Answer:

    class Car { speed: number = 0; } -> Option B
  4. Quick Check:

    Property syntax = name: type = value [OK]
Hint: Use colon for type, equals for default value [OK]
Common Mistakes:
  • Swapping type and value order
  • Missing colon between name and type
  • Using wrong punctuation
3. What will be the output of this TypeScript code?
class Person {
  name: string = "Alice";
  age: number = 30;
}
const p = new Person();
console.log(p.name + ' is ' + p.age + ' years old');
medium
A. "Alice is 30 years old"
B. "undefined is undefined years old"
C. Error: Property 'name' does not exist
D. "Alice is undefined years old"

Solution

  1. Step 1: Understand property initialization

    The class Person has properties name and age initialized with "Alice" and 30 respectively.
  2. Step 2: Check the output of console.log

    Creating an object p sets these properties. The log concatenates them into the string "Alice is 30 years old".
  3. Final Answer:

    "Alice is 30 years old" -> Option A
  4. Quick Check:

    Initialized properties print correct values [OK]
Hint: Default values print as expected when object created [OK]
Common Mistakes:
  • Assuming properties are undefined without constructor
  • Confusing property names or types
  • Expecting runtime errors without syntax mistakes
4. Identify the error in this TypeScript class property declaration:
class Book {
  title string = "Unknown";
  pages: number = 100;
}
medium
A. Class name should be lowercase
B. Missing default value for 'pages' property
C. Missing colon ':' between property name and type in 'title string = "Unknown";'
D. Properties cannot have default values

Solution

  1. Step 1: Check syntax of property declarations

    Property declarations require a colon ':' between the name and type, like title: string.
  2. Step 2: Identify the incorrect line

    The line title string = "Unknown"; misses the colon, causing a syntax error.
  3. Final Answer:

    Missing colon ':' between property name and type in 'title string = "Unknown";' -> Option C
  4. Quick Check:

    Property syntax requires colon ':' [OK]
Hint: Always put ':' between property name and type [OK]
Common Mistakes:
  • Omitting colon between name and type
  • Thinking default values are not allowed
  • Ignoring case sensitivity of class names (not an error)
5. You want to create a TypeScript class Student with a property grades that stores an array of numbers and defaults to an empty array. Which is the correct declaration to ensure each object has its own separate array?
hard
A. class Student { grades: number[] = []; }
B. class Student { static grades: number[] = []; }
C. class Student { grades: number[]; } // no default value
D. class Student { grades = []; }

Solution

  1. Step 1: Understand instance vs static properties

    Instance properties belong to each object separately; static properties are shared by all objects.
  2. Step 2: Check which option creates separate arrays per object

    class Student { grades: number[] = []; } declares grades as an instance property with default empty array, so each object gets its own array.
  3. Step 3: Analyze other options

    class Student { static grades: number[] = []; } uses static, so all objects share one array (not desired). class Student { grades: number[]; } // no default value has no default, so array is undefined unless set later. class Student { grades = []; } misses type annotation explicitly, inferring any[] instead of number[].
  4. Final Answer:

    class Student { grades: number[] = []; } -> Option A
  5. Quick Check:

    Instance property with default array = separate arrays [OK]
Hint: Use instance property with default [] for separate arrays [OK]
Common Mistakes:
  • Using static for properties needing separate values
  • Not initializing arrays causing undefined errors
  • Omitting type annotation (less clear)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard