Lesson 24 of 60 – Default Parameters
40%

Default Parameters in TypeScript

A default parameter is a function parameter that automatically receives a default value when no value is provided during the function call.

Note: Default parameters make functions flexible because the caller does not always need to provide every argument.

1. What is a Default Parameter?

A default parameter has a value that is automatically used when the caller does not provide an argument.

function greet(name: string = "Guest") {
    console.log("Hello " + name);
}

2. Default Parameter Syntax

The default value is assigned using the = operator.

function functionName(
    parameter: type = defaultValue
) {
    // code
}

3. Calling Without an Argument

If the argument is omitted, the default value is used.

function greet(name: string = "Guest") {
    console.log("Hello " + name);
}

greet();

Output:

Hello Guest

4. Calling With an Argument

If an argument is provided, the provided value replaces the default value.

function greet(name: string = "Guest") {
    console.log("Hello " + name);
}

greet("Amit");

Output:

Hello Amit

5. Default Number Parameter

A number parameter can also have a default value.

function showAge(age: number = 18) {
    console.log(age);
}

showAge();

Output:

18

6. Default Boolean Parameter

Boolean parameters can also have default values.

function showStatus(
    active: boolean = true
) {
    console.log(active);
}

showStatus();

7. Default Value with Return Type

A default parameter can be combined with an explicit function return type.

function greet(
    name: string = "Guest"
): string {
    return "Hello " + name;
}

8. Multiple Default Parameters

A function can have multiple parameters with default values.

function student(
    name: string = "Guest",
    age: number = 18
) {
    console.log(name, age);
}

student();

9. Providing One Default Parameter

You can provide only some arguments when using default parameters.

function student(
    name: string = "Guest",
    age: number = 18
) {
    console.log(name, age);
}

student("Rahul");

The name becomes Rahul, while age remains 18.

10. Providing All Arguments

When all arguments are provided, the default values are not used.

function student(
    name: string = "Guest",
    age: number = 18
) {
    console.log(name, age);
}

student("Amit", 22);

11. Default Parameter with Calculation

Default parameters are useful for calculations.

function multiply(
    price: number,
    quantity: number = 1
): number {
    return price * quantity;
}

console.log(multiply(500));

The quantity becomes 1 when it is omitted.

12. Default Parameter with String

Default strings are useful when a value has a common fallback.

function welcome(
    message: string = "Welcome"
): string {
    return message;
}

console.log(welcome());

13. Default Parameter with Array

A function parameter can also have an array as its default value.

function showNumbers(
    numbers: number[] = []
): void {
    console.log(numbers);
}

showNumbers();

14. Default Parameter with Object

Objects can also be used as default parameter values.

function showUser(
    user: { name: string; age: number } = {
        name: "Guest",
        age: 0
    }
): void {
    console.log(user);
}

15. Default Parameters and undefined

A default value is used when the argument is omitted or when undefined is passed.

function greet(
    name: string = "Guest"
) {
    console.log(name);
}

greet();
greet(undefined);

Both calls use "Guest".

16. Default Parameter and null

Passing null is different from omitting the argument.

function greet(
    name: string | null = "Guest"
) {
    console.log(name);
}

greet(null);

Here, null is passed explicitly, so "Guest" is not substituted.

17. Default Parameter with Optional Parameter

A parameter with a default value is automatically optional when calling the function.

function greet(
    name: string = "Guest"
): void {
    console.log(name);
}

greet();
greet("Rahul");

18. Default Parameter vs Optional Parameter

An optional parameter can be undefined when omitted. A default parameter receives a specific fallback value.

function optional(name?: string) {
    console.log(name);
}

function defaultName(name: string = "Guest") {
    console.log(name);
}

19. Default Parameter Before Required Parameter

A required parameter should not come after a parameter with a default value in a way that requires an omitted argument in the middle.

A common and clear pattern is to put required parameters first and default parameters later.

function student(
    name: string,
    age: number = 18
) {
    console.log(name, age);
}

20. Default Parameter with Multiple Types

A default parameter can also use a union type.

function showValue(
    value: string | number = "Unknown"
) {
    console.log(value);
}

showValue();
showValue(100);

21. Default Parameters in Arrow Functions

Arrow functions can also use default parameters.

const greet = (
    name: string = "Guest"
): string => {
    return "Hello " + name;
};

console.log(greet());

22. Default Parameters in React

Default parameters can be useful in React helper functions when a value has a standard fallback.

function getMessage(
    message: string = "Welcome"
): string {
    return message;
}

23. Default Parameters in React Native

React Native functions can use default values for optional configuration or display information.

function showTitle(
    title: string = "Home"
): void {
    console.log(title);
}

24. Default Navigation Value

A default value can be useful when a screen name or other value has a standard fallback.

function openScreen(
    screenName: string = "Home"
): void {
    console.log("Opening:", screenName);
}

openScreen();

25. Default User Role

Default parameters can provide a standard user role.

function createUser(
    name: string,
    role: string = "Student"
): void {
    console.log(name, role);
}

createUser("Amit");

The role becomes "Student".

26. Default Value with Calculation

Default values can simplify calculations where a common value is used frequently.

function calculatePrice(
    price: number,
    quantity: number = 1
): number {

    return price * quantity;
}

console.log(calculatePrice(100));

The result is 100.

27. Common Mistake

A common mistake is expecting a default value when passing another value.

function greet(
    name: string = "Guest"
) {
    console.log(name);
}

greet("Amit");

The output is Amit, not Guest, because a value was provided.

28. When to Use Default Parameters

Default parameters are useful when:

  • A parameter has a common default value.
  • The argument is not always required.
  • You want simpler function calls.
  • You want to avoid manually checking for undefined values.
function sendMessage(
    message: string,
    priority: number = 1
) {
    console.log(message, priority);
}

29. Complete Practical Example

Here is a practical student example using a required parameter and a default parameter.

function createStudent(
    name: string,
    course: string = "ADCA"
): string {

    return name + " is enrolled in " + course;
}

console.log(createStudent("Amit"));

console.log(
    createStudent("Rahul", "Web Development")
);

30. Default Parameters Summary

Default parameters provide automatic values when arguments are not supplied.

function greet(
    name: string = "Guest"
): string {

    return "Hello " + name;
}

console.log(greet());
console.log(greet("Amit"));

Default parameters make functions flexible, reusable, and easier to call.

📌 Key Points

  • A default parameter has a predefined value.
  • Use the = operator to assign a default value.
  • The default value is used when the argument is omitted.
  • A provided argument replaces the default value.
  • Default parameters can be strings, numbers, booleans, arrays, objects, or other types.
  • Default parameters can be combined with explicit return types.
  • Default parameters can be used in arrow functions.
  • They are useful in React and React Native applications.
  • Passing undefined uses the default value.
  • Passing null is different from omitting the argument.

🧠 Quick Quiz

Question: What happens when a function parameter with a default value is omitted?