0
0
Typescriptprogramming~10 mins

Default parameters with types in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default parameters with types
Function call
Check if argument provided
Use arg
Execute function body
Return result
When a function is called, it checks if an argument is given. If not, it uses the default value with the specified type.
Execution Sample
Typescript
function greet(name: string = "Friend") {
  return `Hello, ${name}!`;
}

console.log(greet());
console.log(greet("Alice"));
This function greets a person using a default name if none is provided.
Execution Table
StepFunction CallParameter 'name'ActionOutput
1greet()undefinedUse default 'Friend'Hello, Friend!
2greet("Alice")"Alice"Use provided argumentHello, Alice!
3End---
💡 Function calls complete; default used when argument missing.
Variable Tracker
VariableStartAfter Call 1After Call 2Final
nameundefined"Friend""Alice"-
Key Moments - 2 Insights
Why does the function use "Friend" when no argument is given?
Because the parameter 'name' has a default value "Friend" defined, so when the argument is missing (see execution_table row 1), it uses that default.
What happens if we pass an argument to the function?
The function uses the provided argument instead of the default (see execution_table row 2), so 'name' becomes the passed value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'name' during the first function call?
A"Alice"
B"Friend"
Cundefined
Dnull
💡 Hint
Check the 'Action' column in execution_table row 1.
At which step does the function use the argument passed by the caller?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
Look at the 'Action' column in execution_table row 2.
If we remove the default value, what would happen when calling greet() with no arguments?
AIt uses undefined as name
BIt uses "Friend" anyway
CIt throws an error
DIt returns an empty string
💡 Hint
Think about TypeScript requiring arguments when no default is set.
Concept Snapshot
function f(param: type = defaultValue) {
  // function body
}

- If argument missing, defaultValue is used.
- Parameter type enforces argument type.
- Helps avoid undefined errors and simplifies calls.
Full Transcript
This visual trace shows how TypeScript functions handle default parameters with types. When the function greet is called without an argument, it uses the default string "Friend" as the parameter 'name'. When called with an argument like "Alice", it uses that value instead. The execution table tracks each call, showing parameter values and outputs. The variable tracker shows how 'name' changes. Key moments clarify why defaults are used and what happens when arguments are passed. The quiz tests understanding of parameter values and behavior when defaults are removed.