Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

typeof

Sumário

O operador typeof retorna uma string indicando o tipo de um operando.

Sintaxe

O operador typeof pode ser utilizado das seguintes maneiras:

typeof operando

Parâmetros

operando é a string, variável, keyword, ou objeto para que o tipo do mesmo seja retornado. O uso de parênteses é opicional.

Descrição

Esta tabela resume os possíveis valores que são retornados pelo typeof:

Tipo Resultado
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Host object (provided by the JS environment) Implementation-dependent
Function object (implements [[Call]] in ECMA-262 terms) "function"
E4X XML object "xml"
E4X XMLList object "xml"
Any other object "object"

Exemplos

Casos comuns

// Números
typeof 37 === 'number';
typeof 3.14 === '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!

// Strings
typeof "" === 'string';
typeof "bla" === 'string';
typeof (typeof 1) === 'string'; // typeof always return 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!

// Undefined
typeof undefined === 'undefined';
typeof blabla === 'undefined'; // an undefined variable

// Objetos
typeof {a:1} === 'object';
typeof [1, 2, 4] === 'object'; // use Array.isArray or Object.prototype.toString.call to differentiate regular objects from arrays
typeof new Date() === 'object';

typeof new Boolean(true) === 'object'; // this is confusing. Don't use!
typeof new Number(1) === 'object'; // this is confusing. Don't use!
typeof new String("abc") === 'object';  // this is confusing. Don't use!

// Funções
typeof function(){} === 'function';
typeof Math.sin === 'function';

null

typeof null === 'object'; // This stands since the beginning of JavaScript

Na primeira implementação do JavaScript, valores em JavaScript foram representados com uma tag de tipo e um valor. A tag de tipo para objetos foi 0. null foi representada com o nome NULL (0x00 na maioria das plataformas). Consequentemente, null teve 0 como sua tag de tipo, portanto o typeof retorna esse valor. (necessário referência)

Isso está previsto para ser corrigido na próxima versão do ECMAScript (que estará disponível através de um opt-in). Isso resultará em um typeof null === 'null'.

Expressões regulares

Expressões regulares que podem ser chamadas foram uma adição não-padrão em alguns browsers (necessidade de referência para dizer qual).

typeof /s/ === 'function'; // Chrome 1-12 ... // Non-conform to ECMAScript 5.1
typeof /s/ === 'object'; // Firefox 5+ ...    // Conform to ECMAScript 5.1

Outras peculiaridades

alert versões antigas do Internet Explorer

No IE 6, 7 e 8, typeof alert === 'object'

Especificação

ECMA-262 section 11.4.3

Veja também

Etiquetas do documento e colaboradores

 Colaboradores desta página: teoli, HenriqueSilverio
 Última atualização por: teoli,