typeof
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
typeof
演算子は、オペランドの値の型を示す文字列を返します。
試してみましょう
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 operand
引数
解説
以下はtypeof
が返す可能性がある値の一覧です。型とプリミティブの詳細については、JavaScript のデータ構造のページも参照してください。
型 | 結果 |
---|---|
Undefined | "undefined" |
Null | "object" (理由) |
論理型 | "boolean" |
数値型 | "number" |
長整数型 | "bigint" |
文字列型 | "string" |
シンボル型 | "symbol" |
関数オブジェクト (ECMA-262 の用語では [[Call]] を実装したもの、クラスも関数) | "function" |
その他のオブジェクト | "object" |
この値のリストは網羅的なものです。仕様を遵守するエンジンは、掲載されている値以外を生成する(または過去には生成していた)ことは報告されていません。
例
基本的な使い方
// 数値typeof 37 === "number";typeof 3.14 === "number";typeof 42 === "number";typeof Math.LN2 === "number";typeof Infinity === "number";typeof NaN === "number"; // "Not-A-Number" であるにもかかわらず。typeof Number("1") === "number"; // Number は数値に型強制できない値を含めて、typeof Number("shoe") === "number"; // あらゆるものを数字に解釈します。typeof 42n === "bigint";// 文字列typeof "" === "string";typeof "bla" === "string";typeof `template literal` === "string";typeof "1" === "string"; // 文字列内の数値は文字列型のままです。typeof typeof 1 === "string"; // typeof は常に文字列を返します。typeof String(1) === "string"; // String は何でも文字列に変換するので、toString よりも安全です。// 論理型typeof true === "boolean";typeof false === "boolean";typeof Boolean(1) === "boolean"; // Boolean は、値が真値か偽値かに基づいて変換します。typeof !!1 === "boolean"; // ! (論理 NOT) を 2 回呼び出すと Boolean() と同等になります。// シンボルtypeof Symbol() === "symbol";typeof Symbol("foo") === "symbol";typeof Symbol.iterator === "symbol";// Undefinedtypeof undefined === "undefined";typeof declaredButUndefinedVariable === "undefined";typeof undeclaredVariable === "undefined";// オブジェクト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";
typeof null
// JavaScript の初期からの実装に基づくtypeof null === "object";
JavaScript の最初の実装では、JavaScript の値は型タグと値で表現されていました。オブジェクトの型タグは0
で、null
は NULL ポインター (ほとんどのプラットフォームで0x00
) として表されていました。その結果、null
はタグの型として0
を持っていたため、typeof
の返値は"object"
です。(リファレンス)
ECMAScript の修正案が (オプトインを使用して) 提案されましたが、却下されました。それはtypeof null === "null"
という結果になるものでした。
new 演算子の使用
構文で括弧が必要な場合
typeof
演算子は、優先順位が加算 (+
) などの二項演算子よりも高くなります。そのため、加算結果の型を評価するには括弧が必要です。
// 括弧を使うと、式のデータ型を特定することができます。const someData = 99;typeof someData + " Wisen"; // "number Wisen"typeof (someData + " Wisen"); // "string"
エラー
typeof
は一般に、それが供給されたオペランドの文字列を返すことが保証されています。宣言されていない識別子があっても、typeof
は"undefined"
を返し、エラーは発生しません。
typeof undeclaredVariable; // "undefined"
しかし、typeof
を同じブロック内の字句宣言(let
、const
、class
)の前に使用すると、ReferenceError
が発生します。 ブロックスコープの変数は、ブロックの開始から初期化が処理されるまでの間、一時的なデッドゾーンにあり、その間にアクセスするとエラーが発生します。ブロックスコープ内の変数は、ブロックの開始から初期化が処理されるまで「一時的なデッドゾーン」にあり、その間にアクセスされるとエラーが発生します。
typeof newLetVariable; // ReferenceErrortypeof newConstVariable; // ReferenceErrortypeof newClass; // ReferenceErrorlet newLetVariable;const newConstVariable = "hello";class newClass {}
document.all の例外的な動作
現在のブラウザーではすべて、標準外のホストオブジェクトdocument.all
はundefined
型になります。
typeof document.all === "undefined";
document.all
は偽値でもあり、undefined
と緩い等価性がありますが、undefined
ではありません。document.all
が"undefined"
という型を持つケースは、ウェブ標準では、ウェブ互換性を目的とした ECMAScript 標準の「意図的な違反」として分類されています。
より具体的な型を取得するカスタムメソッド
typeof
は非常に便利ですが、汎用性はそれほど高くありません。たとえば、typeof []
は"object"
になります。typeof new Date()
やtypeof /abc/
などと同様です。
型チェックの精度を上げるために、ここではカスタム関数type(value)
を紹介します。これは主にtypeof
の動作を模倣しますが、プリミティブ型以外のもの(つまりオブジェクトや関数)については、可能な限り詳細な型名を返します。
function type(value) { if (value === null) { return "null"; } const baseType = typeof value; // プリミティブ型 if (!["object", "function"].includes(baseType)) { return baseType; } // Symbol.toStringTag は、多くの場合、オブジェクトのクラスの // 「表示名」を指定する。これは Object.prototype.toString() で使用する。 const tag = value[Symbol.toStringTag]; if (typeof tag === "string") { return tag; } // ソースコードが "class" キーワードで始まる関数である場合 if ( baseType === "function" && Function.prototype.toString.call(value).startsWith("class") ) { return "class"; } // コンストラクターの名前。例えば、`Array`、`GeneratorFunction`、 // `Number`、`String`、`Boolean`、`MyCustomClass`。 const className = value.constructor.name; if (typeof className === "string" && className !== "") { return className; } // ここまでくると、値の型を取得する確実な方法はないため、 // 基本実装を使用します。 return baseType;}
存在しない可能性がある変数を調べるには、ReferenceError
が発生する可能性があるため、typeof nonExistentVar === "undefined"
を使用してください。この動作は、カスタムコードでは模倣できないためです。
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-typeof-operator |