MDNで調べたことを備忘録としてメモします。
現在はMDNからの引用が中心ですが、今後、使ってみて便利だと感じたケースなども追記していきたいと思っています。
typeof 演算子は、未評価のオペランドの型を示す文字列を返します。
example.js
console.log(typeof 42);
// expected output: "number"
console.log(typeof 'blubber');
// expected output: "string"
console.log(typeof true);
// expected output: "boolean"
console.log(typeof undeclaredVariable);
// expected output: "undefined"
構文
typeof 演算子の後に、オペランドを続けて書きます。
typeof operand
typeof(operand)
typeof が返す事が出来る値 (文字列) の一覧表
| 型 | 返値 |
|---|---|
| Undefined | "undefined" |
| Null | "object" |
| 真偽値 | "boolean" |
| 数値 | "number" |
| BigInt (ECMAScript 2020 の新機能) | "bigint" |
| 文字列 | "string" |
| シンボル (ECMAScript 2015 の新機能) | "symbol" |
| Function オブジェクト (implements [[Call]] in ECMA-262 terms) | "function" |
| その他のオブジェクト | "object" |
※Nullの場合も"object"を返す