typeof
Typeof operatörü, değerlendirilmemiş işlenenin türünü gösteren bir dize döndürür.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Sözdizimi
The typeof
operator is followed by its operand:
typeof operand or typeof (operand)
Parametreler
operand
is an expression representing the object or primitive whose type is to be returned.
Parantez isteğe bağlıdır.
Description
The following table summarizes the possible return values of typeof
. For more information about types and primitives, see also the JavaScript data structure page.
Type | Result |
---|---|
Undefined | "undefined" |
Null | "object" (see below) |
Boolean | "boolean" |
Number | "number" |
String | "string" |
Symbol (new in ECMAScript 2015) | "symbol" |
Host object (provided by the JS environment) | Implementation-dependent |
Function object (implements [[Call]] in ECMA-262 terms) | "function" |
Any other object | "object" |
Örnekler
// Sayılar
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'; // but never use this form!
// Metinler
typeof '' === 'string';
typeof 'bla' === '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('abc') === 'string'; // but never use this form!
// Booleans
typeof true === 'boolean';
typeof false === 'boolean';
typeof Boolean(true) === 'boolean'; // but never use this form!
// Symbols
typeof Symbol() === 'symbol'
typeof Symbol('foo') === 'symbol'
typeof Symbol.iterator === 'symbol'
// Undefined
typeof undefined === 'undefined';
typeof declaredButUndefinedVariable === 'undefined';
typeof undeclaredVariable === 'undefined';
// Objeler
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';
// The following is confusing. Don't use!
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String('abc') === 'object';
// Fonksiyonlar
typeof function() {} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';
null
// This stands since the beginning of JavaScript
typeof null === 'object';
In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null
was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the bogus typeof
return value. (reference)
A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in typeof null === 'null'
.
new
operatör kullanımı
// All constructor functions while instantiated with 'new' keyword will always be typeof 'object'
var str = new String('String');
var num = new Number(100);
typeof str; // It will return 'object'
typeof num; // It will return 'object'
// But there is a exception in case of Function constructor of Javascript
var func = new Function();
typeof func; // It will return 'function'
Parantezlere ihtiyaç var
// Parentheses will be very much useful to determine the data type for expressions.
var iData = 99;
typeof iData + ' Wisen'; // It will return 'number Wisen'
typeof (iData + ' Wisen'); // It will return 'string'
Düzenli İfadeler
Callable regular expressions were a non-standard addition in some browsers.
typeof /s/ === 'function'; // Chrome 1-12 Non-conform to ECMAScript 5.1
typeof /s/ === 'object'; // Firefox 5+ Conform to ECMAScript 5.1
Temporal Dead Zone errors
Before ECMAScript 2015, typeof
was always guaranteed to return a string for any operand it was supplied with. But with the addition of non-hoisted, block-scoped let
and const
, using typeof
on let
and const
variables in a block before they are declared will throw a ReferenceError
. This is in contrast with undeclared variables, for which typeof
will return 'undefined'. Block scoped variables are in a "temporal dead zone" from the start of the block until the initialization is processed, during which, it will throw an error if accessed.
typeof undeclaredVariable === 'undefined';
typeof newLetVariable; let newLetVariable; // ReferenceError
typeof newConstVariable; const newConstVariable = 'hello'; // ReferenceError
İstisnalar
All current browsers expose a non-standard host object document.all
(en-US) with type Undefined.
typeof document.all === 'undefined';
Although the specification allows custom type tags for non-standard exotic objects, it requires those type tags to be different from the predefined ones. The case of document.all
having type tag 'undefined'
must be classified as an exceptional violation of the rules.
Özellikler
Specification | Status | Comment |
---|---|---|
ECMAScript (ECMA-262) The definition of 'The typeof Operator' in that specification. |
Living Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'The typeof Operator' in that specification. |
Standard | |
ECMAScript 5.1 (ECMA-262) The definition of 'The typeof Operator' in that specification. |
Standard | |
ECMAScript 3rd Edition (ECMA-262) The definition of 'The typeof Operator' in that specification. |
Standard | |
ECMAScript 1st Edition (ECMA-262) The definition of 'The typeof Operator' in that specification. |
Standard | Initial definition. Implemented in JavaScript 1.1. |
Browser compatibility
BCD tables only load in the browser
IE özel notlar
On IE 6, 7, and 8 a lot of host objects are objects and not functions. For example:
typeof alert === 'object'