0
0
Typescriptprogramming~10 mins

Null and undefined types in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Null and undefined types
Declare variable
Assign undefined?
YesValue is undefined
Use variable
Assign null?
YesValue is null
Use variable
Assign other value
This flow shows how a variable can be declared and assigned either undefined, null, or another value, and then used.
Execution Sample
Typescript
let a: number | null | undefined;
a = undefined;
a = null;
a = 5;
This code declares a variable that can hold a number, null, or undefined, then assigns these values step-by-step.
Execution Table
StepCode executedVariable 'a' valueType of 'a'Notes
1let a: number | null | undefined;undefinedundefinedDeclared but not assigned, default is undefined
2a = undefined;undefinedundefinedExplicitly assigned undefined
3a = null;nullobjectAssigned null value
4a = 5;5numberAssigned number value
5End of code5numberFinal value is 5
💡 Code ends after assigning 5 to 'a'
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
aundefinedundefinednull55
Key Moments - 3 Insights
Why does 'a' start as undefined even though we didn't assign a value?
In TypeScript, declared variables without an initial value are undefined by default, as shown in execution_table step 1.
Can 'a' hold both null and undefined at the same time?
No, 'a' can hold either null or undefined or a number at a time, but not both simultaneously, as seen in steps 2 and 3.
Why do we need to specify 'number | null | undefined' for 'a'?
Because TypeScript is strict about types, we must explicitly allow null and undefined if we want 'a' to hold those values, shown in the declaration at step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value and type of 'a'?
AValue: null, Type: null
BValue: undefined, Type: undefined
CValue: 5, Type: number
DValue: null, Type: undefined
💡 Hint
Check the 'Variable 'a' value' and 'Type of 'a'' columns at step 3 in execution_table.
At which step does 'a' change from undefined to a number?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Variable 'a' value' column in execution_table and find when it becomes 5.
If we remove '| null | undefined' from the declaration, what would happen when assigning null or undefined?
ANo error, assignments work fine
BError, because 'a' can only be number
CVariable 'a' becomes any type
DVariable 'a' defaults to null
💡 Hint
Recall the declaration in execution_table step 1 and TypeScript's strict typing rules.
Concept Snapshot
TypeScript variables can hold null or undefined only if declared so.
Declare with union types like number | null | undefined.
Variables start as undefined if not assigned.
Assigning null or undefined requires explicit type allowance.
Use these types to handle missing or empty values safely.
Full Transcript
This visual execution shows how a TypeScript variable declared as number | null | undefined changes its value step-by-step. Initially, the variable 'a' is declared without assignment, so it is undefined. Then it is explicitly assigned undefined, then null, and finally a number 5. The variable tracker shows these changes clearly. Key moments explain why variables start as undefined, why they cannot hold null and undefined simultaneously, and why explicit union types are needed. The quiz tests understanding of variable values and types at each step and the importance of type declarations.