typeof
Baseline
Weitgehend verfügbar
Diese Funktion ist gut etabliert und funktioniert auf vielen Geräten und in vielen Browserversionen. Sie ist seit Juli 2015 browserübergreifend verfügbar.
Der typeof-Operator gibt einen String zurück, der den Typ des Werts seines Operanden angibt.
Probieren Sie es aus
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"
Syntax
typeof operand
Parameter
operand-
Ein Ausdruck mit einer Priorität von 14 (Präfixoperatoren) oder höher, der das Objekt oder den primitiven Wert repräsentiert, dessen Typ zurückgegeben werden soll.
Rückgabewert
Ein String, der den Typ von operand angibt.
| Typ | Ergebnis |
|---|---|
| Undefined | "undefined" |
| Null | "object" (Grund) |
| Boolean | "boolean" |
| Number | "number" |
| BigInt | "bigint" |
| String | "string" |
| Symbol | "symbol" |
| Function (implementiert [[Call]] gemäß ECMA-262; auch Klassen sind Funktionen) | "function" |
| Jedes andere Objekt | "object" |
Diese Liste von Werten ist vollständig. Es sind keine spezifikationskonformen Engines bekannt, die andere als die aufgeführten Werte zurückgeben oder in der Vergangenheit zurückgegeben haben.
Weitere Informationen zu Typen und primitiven Werten finden Sie auf der Seite JavaScript-Datenstrukturen.
Beispiele
>Grundlegende Verwendung
// 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 Number("shoe") === "number"; // including values that cannot be type coerced to a number
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";
// use Array.isArray or Object.prototype.toString.call
// to differentiate regular objects from arrays
typeof [1, 2, 4] === "object";
typeof new Date() === "object";
typeof /regex/ === "object";
// The following are confusing, dangerous, and wasteful. Avoid them.
typeof new Boolean(true) === "object";
typeof new Number(1) === "object";
typeof new String("abc") === "object";
// Functions
typeof function () {} === "function";
typeof class C {} === "function";
typeof Math.sin === "function";
typeof null
// This stands since the beginning of JavaScript
typeof null === "object";
In der ersten Implementierung von JavaScript wurden JavaScript-Werte durch ein Typ-Tag und einen Wert repräsentiert. Das Typ-Tag für Objekte war 0. null wurde als NULL-Zeiger repräsentiert (0x00 auf den meisten Plattformen). Folglich hatte null das Typ-Tag 0, weshalb typeof den Wert "object" zurückgibt. (Quelle)
Für ECMAScript wurde eine Korrektur vorgeschlagen (als Opt-in), die jedoch abgelehnt wurde. Sie hätte dazu geführt, dass typeof null === "null" gilt.
Verwendung des new-Operators
Alle Konstruktorfunktionen, die mit new aufgerufen werden, geben nicht-primitive Werte zurück ("object" oder "function"). Die meisten geben Objekte zurück. Eine bemerkenswerte Ausnahme ist Function, das eine Funktion zurückgibt.
const str = new String("String");
const num = new Number(100);
typeof str; // "object"
typeof num; // "object"
const func = new Function();
typeof func; // "function"
Warum Klammern in der Syntax nötig sind
Der typeof-Operator hat eine höhere Priorität als binäre Operatoren wie die Addition (+). Daher sind Klammern erforderlich, um den Typ des Ergebnisses einer Addition zu ermitteln.
// Parentheses can be used for determining the data type of expressions.
const someData = 99;
typeof someData + " foo"; // "number foo"
typeof (someData + " foo"); // "string"
Zusammenspiel mit nicht deklarierten und nicht initialisierten Variablen
typeof funktioniert auch mit nicht deklarierten Bezeichnern und gibt "undefined" zurück, statt einen Fehler auszulösen.
typeof undeclaredVariable; // "undefined"
Wird typeof jedoch im selben Block vor der Deklaration auf lexikalische Deklarationen (let, const, using, await using und class) angewendet, wird ein ReferenceError ausgelöst. Variablen mit Block-Scope befinden sich vom Beginn des Blocks bis zum Abschluss ihrer Initialisierung in einer temporären toten Zone. Ein Zugriff während dieser Zeit löst einen Fehler aus.
typeof newLetVariable; // ReferenceError
typeof newConstVariable; // ReferenceError
typeof newClass; // ReferenceError
let newLetVariable;
const newConstVariable = "hello";
class newClass {}
Weitere Einzelheiten finden Sie unter typeof-Operator und undefined.
Besonderes Verhalten von document.all
Alle aktuellen Browser stellen ein nicht standardisiertes Host-Objekt document.all mit dem Typ undefined bereit.
typeof document.all === "undefined";
Obwohl document.all außerdem falsy und lose gleich undefined ist, ist es nicht undefined. Dass document.all den Typ "undefined" hat, wird in den Webstandards als „willentliche Abweichung“ vom ursprünglichen ECMAScript-Standard zugunsten der Webkompatibilität eingestuft.
Benutzerdefinierte Methode zur Ermittlung eines genaueren Typs
typeof ist sehr nützlich, aber nicht so vielseitig, wie es mitunter erforderlich wäre. Beispielsweise ist sowohl typeof [] als auch typeof new Date() und typeof /abc/ gleich "object".
Für eine genauere Typprüfung wird hier eine benutzerdefinierte Funktion type(value) vorgestellt. Sie ahmt das Verhalten von typeof weitgehend nach, gibt aber für nicht-primitive Werte (also Objekte und Funktionen) nach Möglichkeit eine genauere Typbezeichnung zurück.
function type(value) {
if (value === null) {
return "null";
}
const baseType = typeof value;
// Primitive types
if (!["object", "function"].includes(baseType)) {
return baseType;
}
// Symbol.toStringTag often specifies the "display name" of the
// object's class. It's used in Object.prototype.toString().
const tag = value[Symbol.toStringTag];
if (typeof tag === "string") {
return tag;
}
// If it's a function whose source code starts with the "class" keyword
if (
baseType === "function" &&
Function.prototype.toString.call(value).startsWith("class")
) {
return "class";
}
// The name of the constructor; for example `Array`, `GeneratorFunction`,
// `Number`, `String`, `Boolean` or `MyCustomClass`
const className = value.constructor?.name;
if (typeof className === "string" && className !== "") {
return className;
}
// At this point there's no robust way to get the type of value,
// so we use the base implementation.
return baseType;
}
Verwenden Sie zur Prüfung möglicherweise nicht existierender Variablen, deren Zugriff sonst einen ReferenceError auslösen würde, typeof nonExistentVar === "undefined", da sich dieses Verhalten mit benutzerdefiniertem Code nicht nachbilden lässt.
Spezifikationen
| Spezifikation |
|---|
| ECMAScript® 2027 Language Specification> # sec-typeof-operator> |