A default parameter is a function parameter that automatically receives a default value when no value is provided during the function call.
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);
}
The default value is assigned using the
= operator.
function functionName(
parameter: type = defaultValue
) {
// code
}
If the argument is omitted, the default value is used.
function greet(name: string = "Guest") {
console.log("Hello " + name);
}
greet();
Output:
Hello Guest
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
A number parameter can also have a default value.
function showAge(age: number = 18) {
console.log(age);
}
showAge();
Output:
18
Boolean parameters can also have default values.
function showStatus(
active: boolean = true
) {
console.log(active);
}
showStatus();
A default parameter can be combined with an explicit function return type.
function greet(
name: string = "Guest"
): string {
return "Hello " + name;
}
A function can have multiple parameters with default values.
function student(
name: string = "Guest",
age: number = 18
) {
console.log(name, age);
}
student();
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.
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);
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.
Default strings are useful when a value has a common fallback.
function welcome(
message: string = "Welcome"
): string {
return message;
}
console.log(welcome());
A function parameter can also have an array as its default value.
function showNumbers(
numbers: number[] = []
): void {
console.log(numbers);
}
showNumbers();
Objects can also be used as default parameter values.
function showUser(
user: { name: string; age: number } = {
name: "Guest",
age: 0
}
): void {
console.log(user);
}
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".
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.
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");
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);
}
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);
}
A default parameter can also use a union type.
function showValue(
value: string | number = "Unknown"
) {
console.log(value);
}
showValue();
showValue(100);
Arrow functions can also use default parameters.
const greet = (
name: string = "Guest"
): string => {
return "Hello " + name;
};
console.log(greet());
Default parameters can be useful in React helper functions when a value has a standard fallback.
function getMessage(
message: string = "Welcome"
): string {
return message;
}
React Native functions can use default values for optional configuration or display information.
function showTitle(
title: string = "Home"
): void {
console.log(title);
}
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();
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".
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.
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.
Default parameters are useful when:
function sendMessage(
message: string,
priority: number = 1
) {
console.log(message, priority);
}
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")
);
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.
= operator to assign a default value.undefined uses the default value.null is different from omitting the argument.Question: What happens when a function parameter with a default value is omitted?