Basics

Challenges

Classes

Cookies

Debugging

Events

External Files

Flow Control

Forms

Functions

Html Elements

Installation

Interfaces

Keywords

Modules

Namespaces

Operators

Reference Files

String

Types

TypeScript - Functions with default values for their arguments

If there's any risk of of not getting one ore more of the arguments defined in the function's signature, you can set default values for your function arguments. By doing this, you make the argument optional.

Notice that you can leave off the third and even the second argument.

View the Console


function SayHello(name: string, feeling: string = "good", hungry?: boolean): void
{
    console.log("**************************");
    console.log(`Hello ${name}`);
    if(feeling ==="good")
    {
        console.log("I'm glad you are feeling good.");
    }
    else
    {
        console.log("I hope you are feeling good.");
    }

    if(hungry !== undefined)
    {
        console.log(`Are you hungry? ${hungry}`);
    }
}

SayHello("Ricky Bobby", "so-so", true);
SayHello("Ricky Bobby", "so-so");
SayHello("Ricky Bobby");