typeof

  • Revision slug: Web/JavaScript/Reference/Operators/typeof
  • Revision title: typeof
  • Revision id: 487287
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Editorial review

Revision Content

Summary

The typeof operator returns a string indicating the type of the unevaluated operand.

Operator
Implemented in: JavaScript 1.1
ECMA Version: ECMA-262 (and ECMA-357 for E4X objects)

Syntax

The typeof operator is followed by its operand:

typeof operand

Parameters

operand is an expression representing the object or primitive whose type is to be returned.

Description

This table summarizes the possible return values of typeof:

Type Result
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"

Examples

Normal cases

// Numbers
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

// Objects
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!

// Functions
typeof function(){} === 'function';
typeof Math.sin === 'function';

null

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

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 is 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'.

Regular expressions

Callable regular expressions were a non-standard addition in some browsers (need reference to say which).

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

Other quirks

alert in old Internet Explorer versions

On IE 6, 7, and 8, typeof alert === 'object'

Note: Not a quirk. It's telling the truth. Lots of old IE host objects are objects not functions

Specification

ECMA-262 section 11.4.3

See also

Revision Source

<h2 id="Summary">Summary</h2>
<p>The <code>typeof</code> operator returns a string indicating the type of the unevaluated operand.</p>
<table class="standard-table">
 <thead>
  <tr>
   <th colspan="2" scope="col">Operator</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>Implemented in:</td>
   <td>JavaScript 1.1</td>
  </tr>
  <tr>
   <td>ECMA Version:</td>
   <td>ECMA-262 (and ECMA-357 for E4X objects)</td>
  </tr>
 </tbody>
</table>
<h2 id="Syntax">Syntax</h2>
<p>The <code>typeof</code> operator is followed by its operand:</p>
<pre>
<code>typeof <code><em>operand</em></code></code></pre>
<p><span style="font-family: Georgia, Times, 'Times New Roman', serif; font-size: 1.428em; line-height: 1.572;">Parameters</span></p>
<p><code><em>operand</em></code> is an expression representing the object or primitive whose type is to be returned.</p>
<h2 id="Description">Description</h2>
<p>This table summarizes the possible return values of <code>typeof</code>:</p>
<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Type</th>
   <th scope="col">Result</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>Undefined</td>
   <td><code>"undefined"</code></td>
  </tr>
  <tr>
   <td>Null</td>
   <td><code>"object"</code></td>
  </tr>
  <tr>
   <td>Boolean</td>
   <td><code>"boolean"</code></td>
  </tr>
  <tr>
   <td>Number</td>
   <td><code>"number"</code></td>
  </tr>
  <tr>
   <td>String</td>
   <td><code>"string"</code></td>
  </tr>
  <tr>
   <td>Host object (provided by the JS environment)</td>
   <td><em>Implementation-dependent</em></td>
  </tr>
  <tr>
   <td>Function object (implements [[Call]] in ECMA-262 terms)</td>
   <td><code>"function"</code></td>
  </tr>
  <tr>
   <td>E4X XML object</td>
   <td><code>"xml"</code></td>
  </tr>
  <tr>
   <td>E4X XMLList object</td>
   <td><code>"xml"</code></td>
  </tr>
  <tr>
   <td>Any other object</td>
   <td><code>"object"</code></td>
  </tr>
 </tbody>
</table>
<h2 id="Examples">Examples</h2>
<h3 id="Normal_cases">Normal cases</h3>
<pre class="brush:js">
// Numbers
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

// Objects
typeof {a:1} === 'object';
typeof [1, 2, 4] === 'object'; // use <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray" title="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray">Array.isArray</a> 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!

// Functions
typeof function(){} === 'function';
typeof Math.sin === 'function';
</pre>
<h3 id="null"><code>null</code></h3>
<pre class="brush:js">
typeof null === 'object'; // This stands since the beginning of JavaScript
</pre>
<p>In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. <code>null</code> was represented as the NULL pointer (0x00 is most platforms). Consequently, null had 0 as type tag, hence the bogus <code>typeof</code> return value. (<a href="http://www.2ality.com/2013/10/typeof-null.html">reference</a>)</p>
<p>A fix was proposed for ECMAScript (via an opt-in), but&nbsp;<a class="external" href="http://wiki.ecmascript.org/doku.php?id=harmony:typeof_null">was rejected</a>. It would have resulted in <code>typeof null === 'null'</code>.</p>
<h3 id="Regular_expressions">Regular expressions</h3>
<p>Callable regular expressions were a non-standard addition in some browsers (need reference to say which).</p>
<pre class="brush:js">
typeof /s/ === 'function'; // Chrome 1-12 ... // Non-conform to ECMAScript 5.1
typeof /s/ === 'object'; // Firefox 5+ ...    // Conform to ECMAScript 5.1
</pre>
<h3 id="Other_quirks">Other quirks</h3>
<h4 id="alert_in_old_Internet_Explorer_versions"><code>alert</code> in old Internet Explorer versions</h4>
<p>On IE 6, 7, and 8, <code>typeof alert === 'object'</code></p>
<div class="note">
 <p>Note: Not a quirk. It's telling the truth. Lots of old IE host objects are objects not functions</p>
</div>
<h2 id="Specification">Specification</h2>
<p><a class="external" href="http://ecma-international.org/ecma-262/5.1/#sec-11.4.3">ECMA-262 section 11.4.3</a></p>
<h2 id="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/JavaScript/Reference/Operators/instanceof" title="/en-US/docs/JavaScript/Reference/Operators/instanceof">instanceof</a></li>
</ul>
Revert to this revision