Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more type examples for the typeof operator in JavaSccript #3291

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,16 @@ To learn the type of a variable, use `typeof`:
| string | `typeof s === "string"` |
| number | `typeof n === "number"` |
| boolean | `typeof b === "boolean"` |
| null | `typeof null === "object"` |
| undefined | `typeof undefined === "undefined"` |
| object | `typeof o === "object"` |
| function | `typeof f === "function"` |
| array | `Array.isArray(a)` |
| symbol | `typeof s === "symbol"` |
| bigint | `typeof b === "bigint"` |

For example, you can make a function return different values depending on whether it is passed a string or an array:
You might be wondering why `typeof null` is `object` in JavaScript, and that is an historical accident which will be mentioned again later.
Back to unions, you can make a function return different values depending on whether it is passed a string or an array:

<!-- prettier-ignore -->
```ts twoslash
Expand Down