このロケールの翻訳が存在しないため、英語バージョンのコンテンツを表示しています。 この記事の翻訳にご協力ください!
typeof 演算子は、未評価のオペランドの型を示す文字列を返します。
このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 https://github.com/mdn/interactive-examples をクローンしてプルリクエストを送信してください。
構文
typeof 演算子の後に、オペランドを続けて書きます。
typeof operand
引数
operand は、オブジェクトもしくは primitive 型を返す式を表します。
かっこはオプションです。
解説
以下は typeof が返す事が出来る値 (文字列) の一覧表です。 型とプリミティブの詳細については、JavaScript のデータ構造のページも参照してください。
| 型 | 返値 |
|---|---|
| 未定義 | "undefined" |
| Null | "object" (下記参照) |
| 真偽値 | "boolean" |
| 数値 | "number" |
| BigInt | "bigint" |
| 文字列 | "string" |
| シンボル (ECMAScript6 で新しく導入) | "symbol" |
| ホストオブジェクト (provided by the JS environment) | 実装に依存 |
| 関数オブジェクト (implements [[Call]] in ECMA-262 terms) | "function" |
| その他のオブジェクト | "object" |
例
// Numbers
typeof 37 === 'number';
typeof 3.14 === 'number';
typeof(42) === 'number';
typeof Math.LN2 === 'number';
typeof Infinity === 'number';
typeof NaN === 'number'; // Despite being "Not-A-Number"
typeof Number('1') === 'number'; // Number tries to parse things into numbers
typeof 42n === 'bigint';
// Strings
typeof '' === 'string';
typeof 'bla' === 'string';
typeof `template literal` === 'string';
typeof '1' === 'string'; // note that a number within a string is still typeof string
typeof (typeof 1) === 'string'; // typeof always returns a string
typeof String(1) === 'string'; // String converts anything into a string, safer than toString
// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(1) === 'boolean'; // Boolean() will convert values based on if they're truthy or falsy
typeof !!(1) === 'boolean'; // two calls of the ! (logical NOT) operator are equivalent to Boolean()
// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'
// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';
// Objects
typeof {a: 1} === 'object';
// 通常のオブジェクトと配列を区別するには Array.isArray
// または Object.prototype.toString.call を使用してください
typeof [1, 2, 4] === 'object';
typeof new Date() === 'object';
typeof /regex/ === 'object'; // 過去の結果は正規表現の節を見てください
// 以下のものは紛らわしく、危険で、意味がありません。使用しないでください。
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String('abc') === 'object';
// 関数
typeof function() {} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';
追加情報
null
// JavaScript の初期からの実装に基づく typeof null === 'object';
JavaScript の最初の実装では、 JavaScript の値は型タグと値として表現されていました。オブジェクトの型タグは 0 でした。 null は NULL ポインタ (ほとんどのプラットフォームで 0x00) として表されていました。その結果、 null は型タグとして 0 を持ち、偽の typeof 返値を返します。(リファレンス)
ECMAScript の修正案が (オプトインを使用して) 提案されましたが、却下されました。それは typeof null === 'null' という結果になるものでした。
new 演算子の使用
// コンストラクター関数はすべて、関数コンストラクターを除いて、常に typeof 'object' になります
var str = new String('String');
var num = new Number(100);
typeof str; // 'object' を返す
typeof num; // 'object' を返す
var func = new Function();
typeof func; // 'function' を返す
構文で括弧が必要な場合
// 式のデータ型を特定するために、かっこを使用することができます。 var iData = 99; typeof iData + ' Wisen'; // 'number Wisen' typeof (iData + ' Wisen'); // 'string'
正規表現
呼び出し可能な正規表現は、一部のブラウザーでは非標準的なものでした。
typeof /s/ === 'function'; // Chrome 1-12 Non-conform to ECMAScript 5.1 typeof /s/ === 'object'; // Firefox 5+ Conform to ECMAScript 5.1
エラー
ECMAScript 2015 より前では、 typeof は常にそれが供給されたオペランドの文字列を返すことが保証されていました。宣言されていない識別子があっても、typeof は 'undefined' を返します。typeof を使用すると、エラーは発生しません。
しかしブロックされていない let と const を追加することで、let と const 変数で typeof を使用するか (またはクラスの typeof を使用する)、それらが宣言される前にブロック内で ReferenceError がスローされます。ブロックスコープされた変数は、ブロックの開始から初期化が処理されるまでの「一時的な不感地帯」にあり、その間にアクセスされるとエラーになります。
typeof undeclaredVariable === 'undefined';
typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = 'hello';
class newClass{};
例外
現在のブラウザーではすべて、標準外のホストオブジェクト document.all は undefined 型になります。
typeof document.all === 'undefined';
この仕様書では、非標準エキゾチックオブジェクトのカスタムタイプタグが可能ですが、事前に定義されたタイプタグとは異なるタイプタグが必要です。'undefined' タイプの document.all のケースは、元の ECMA JavaScript 標準の「故意の違反」としてウェブ標準に分類されています。
仕様書
ブラウザーの対応
| デスクトップ | モバイル | サーバー | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
typeof | Chrome 完全対応 1 | Edge 完全対応 12 | Firefox 完全対応 1 | IE 完全対応 3 | Opera 完全対応 あり | Safari 完全対応 あり | WebView Android 完全対応 1 | Chrome Android 完全対応 18 | Firefox Android 完全対応 4 | Opera Android 完全対応 あり | Safari iOS 完全対応 あり | Samsung Internet Android 完全対応 1.0 | nodejs 完全対応 あり |
凡例
- 完全対応
- 完全対応
IE 特有のメモ
IE 6、7、8 では、以下のように多くのホストオブジェクトがオブジェクト型であり、関数ではありません。
typeof alert === 'object'