optional/default parameters
๐ Optional Parameters
optional parameter
x?: T
is of typeT | undefined
.default parameter
x = value
is of typeT
(supposevalue
is of typeT
).
const { log } = console;
// โญ๏ธ optional parameters
// --------------------
// โญ๏ธ `x` is of type `number | undefined` โ๏ธ
// (similar to Optionals in Swift)
function f(x?: number) {
log( x ?? 0 );
}
f(); // 0
f(10); // 10
// โญ๏ธ default parameters
// -------------------
// โญ๏ธ `x` is of type `number`
function g(x = 4){
log(x);
}
g(); // 4
g(5); // 5
g(undefined); // 4 === g()
Last updated
Was this helpful?