Lesson not found
Lesson Progress: 0%
Default parameters let you specify a fallback value for a function parameter when the caller does not provide one. You write the default value right after the parameter firstName using an equals sign, like name: string = "Guest". If the caller passes an argument, the default is ignored and the passed value is used. If the caller skips that argument, the default value is used instead. In TypeScript, the default value must match the parameter's type annotation, so the compiler can still check for type safety. Default parameters make your functions more flexible because they work both with and without arguments. They also help you avoid errors from missing arguments by ensuring a sensible value is always available.
function greet(firstName: string = "Guest"): string {
return "Hello " + firstName;
}
console.log(greet("Alice"));
console.log(greet());Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
A default parameter provides a fallback string when no argument is passed.
firstName has a default value of "Guest".greet("Alice") is called, the argument "Alice" overrides the default.greet() is called with no argument, firstName gets the default "Guest".: string, and the default "Guest" is a string, so the type is always satisfied.function multiply(a: number, b: number = 2): number {
return a * b;
}
console.log(multiply(5));
console.log(multiply(5, 3));Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
A default parameter can be a number, and it works alongside required parameters.
b has a default of 2, while a has no default.multiply(5) uses the default 2 for b, giving 5 * 2 = 10.multiply(5, 3) overrides the default with 3, giving 5 * 3 = 15.function createUser(firstName: string, isAdmin: boolean = false): string {
if (isAdmin) {
return firstName + " (admin)";
}
return firstName + " (user)";
}
console.log(createUser("Alice"));
console.log(createUser("Bob", true));Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
Default parameters work with boolean values too, letting you set a default state.
isAdmin defaults to false, so most users are not admins by default.createUser("Alice") skips the second argument, so isAdmin is false.createUser("Bob", true) passes true, making Bob an admin.if statement checks isAdmin to choose the right message.function logMessage(msg: string, prefix: string = "Info"): string {
return prefix + ": " + msg;
}
console.log(logMessage("Started"));
console.log(logMessage("Failed", "Error"));Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
Default parameters can be used to set a prefix or label, making output customizable.
prefix defaults to "Info", so messages get a standard label.logMessage("Started") uses the default prefix and returns "Info: Started".logMessage("Failed", "Error") overrides the prefix with "Error".msg is required, so you must always provide a message.function calculateTotal(price: number, tax: number = 0.1): number {
return price + price * tax;
}
console.log(calculateTotal(100));
console.log(calculateTotal(100, 0.2));Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
Default parameters can hold values used in calculations, like a default tax rate.
tax defaults to 0.1, representing a 10 percent tax.calculateTotal(100) uses the default tax, giving 100 + 100 * 0.1 = 110.calculateTotal(100, 0.2) overrides the tax rate to 20 percent, giving 120.price must always be provided.const greet6 = (firstName: string = "Guest"): string => "Hi " + firstName;
console.log(greet6("Alice"));
console.log(greet6());Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
Arrow functions can also have default parameters, keeping the concise syntax while adding flexibility.
= to set a default value for firstName right in the parameter list."Alice", the default is overridden and the output is "Hi Alice"."Guest" is used, giving "Hi Guest".Test Incomplete