# optional/default parameters

{% tabs %}
{% tab title="📗 參考" %}
* 📘 [Optional Parameters](https://www.typescriptlang.org/docs/handbook/2/functions.html#optional-parameters)
{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
* optional parameter **`x?: T`** is of type **`T | undefined`**.
* default parameter **`x = value`** is of type **`T`** (suppose **`value`** is of type **`T`**).
{% endhint %}

```typescript
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()
```
