typeof演算子の使い方
returnValue = typeof ___;if (typeof ___ === 'string') { /* 何か処理 */ }typeof演算子への入力別返り値
| 種類 | 返り値 | 当てはまる例 |
|---|---|---|
| 数値 | "number" | 635, -28.1, 1e3, 0x5fff, NaN, Infinity, Number('41') |
| 文字列 | "string" | "foo bar'', "", `テンプレート文字列`, String(), Date() |
| 真偽値 | "boolean" | true, false, Boolean() |
| 配列 | "object" | [], Array(), new Array() |
| Null | "object" | null |
| 未定義 | "undefined" | undefined |
| 関数 | "function" | function() {}, class Foo {}, toString |
| Symbol | "symbol" | Symbol(), Symbol.iterator |
| BigInt | "bigint" | 635n, BigInt(7), BigInt('41') |
| 正規表現 | "object" | /regexp/g, RegExp('r'), new RegExp('r') |
| その他のオブジェクト | "object" | {foo: 635}, Object(), new Object(), new Date() |
注意すべきパターン逆引き
| 指定値または種類 | 出力 | |
|---|---|---|
null | "object" | |
NaN | "number" | |
| 配列 | "object" | |
Element.attributes | "object" | |
document.querySelectorAll(___) | "object" | |
document.getElementByClassName(___) | "object" | |
document.all | "undefined" | |
new Boolean() | "object" | |
new String() | "object" | |
new Number() | "object" |
document.allは非推奨なので、document.querySelectorAll('*')を使ってください。
参考リンク