# Truthy

JavaScriptは、特定の場所(例えば、`if` 条件文とbooleanの`&&` `||` オペレータ)で、Trueと評価される値(`truthy`)の概念を持っています。次に示すものは、JavaScriptにおいてtruthyです。例えば`0`以外の数値はtruthyです。

```typescript
if (123) { // Will be treated like `true`
  console.log('Any number other than 0 is truthy');
}
```

truthyでないものは、`falsy`と呼ばれます。

これは開発者のための便利なリファレンスです。

| 変数の型                             | *falsyな値*   | *truthyな値* |
| -------------------------------- | ----------- | ---------- |
| `boolean`                        | `false`     | `true`     |
| `string`                         | `''` (空文字列) | その他の文字列    |
| `number`                         | `0` `NaN`   | その他の数値     |
| `null`                           | 常にfalsy     | なし         |
| `undefined`                      | 常にfalsy     | なし         |
| その他のオブジェクト(`{}`や`[]`といった空のものも含む) | なし          | 常にtruthy   |

## Booleanとして扱うことを明示的にする

> The `!!` pattern

一般的に、`boolean`として扱われる値を、それを本当の`boolean`(`true`|`false`)に明示的に変換することは、良いことです。あなたは、`!!`を使って値を本当のbooleanに簡単に変換できます。例えば、`!!foo`です。これは単に`!`を2回使っただけです。最初の`!`は値をbooleanに変換しますが、その論理値を反転します。2つ目の`!`は、その値を本来の値にマッチするよう再度反転させます(例えば、 *truthy* -`!`> `false` -`!`> `true`)。

これはあらゆる場面で共通的に使えるパターンです。

```javascript
// 値を他のものに移す
const hasName = !!name;

// オブジェクトのメンバとして利用する
const someObj = {
  hasName: !!name
}

// 例： ReactJS JSX
{!!someName && <div>{someName}</div>}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://typescript-jp.gitbook.io/deep-dive/recap/truthy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
