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.

Revision 1040396 of typeof

  • Revision slug: Web/JavaScript/Reference/Operators/typeof
  • Revision title: typeof
  • Revision id: 1040396
  • Created:
  • Creator: raq929
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("Operators")}}

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

Syntax

The typeof operator is followed by its operand:

typeof operand

Parameters

operand is an expression representing the object or {{Glossary("Primitive", "primitive")}} whose type is to be returned.

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"

 

Examples

// 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 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'; 


// 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';


// The following is confusing. Don't use!
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';

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

Regular expressions

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

Specifications

Specification Status Comment
{{SpecName('ESDraft', '#sec-typeof-operator', 'The typeof Operator')}} {{Spec2('ESDraft')}}  
{{SpecName('ES6', '#sec-typeof-operator', 'The typeof Operator')}} {{Spec2('ES6')}}  
{{SpecName('ES5.1', '#sec-11.4.3', 'The typeof Operator')}} {{Spec2('ES5.1')}}  
{{SpecName('ES3', '#sec-11.4.3', 'The typeof Operator')}} {{Spec2('ES3')}}  
{{SpecName('ES1', '#sec-11.4.3', 'The typeof Operator')}} {{Spec2('ES1')}} Initial definition. Implemented in JavaScript 1.1.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

IE-specific notes

On IE 6, 7, and 8 a lot of host objects are objects and not functions. For example:

typeof alert === 'object'

See also

Revision Source

<div>{{jsSidebar("Operators")}}</div>

<p>The <strong><code>typeof</code> operator</strong> returns a string indicating the type of the unevaluated operand.</p>

<h2 id="Syntax">Syntax</h2>

<p>The <code>typeof</code> operator is followed by its operand:</p>

<pre class="syntaxbox">
<code>typeof <code><em>operand</em></code></code></pre>

<h3 id="Parameters">Parameters</h3>

<p><code><em>operand</em></code> is an expression representing the object or {{Glossary("Primitive", "primitive")}} whose type is to be returned.</p>

<h2 id="Description">Description</h2>

<p>The following table summarizes the possible return values of <code>typeof</code>. For more information about types and primitives, see also the <a href="/en-US/docs/Web/JavaScript/Data_structures">JavaScript data structure</a> page.</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>(see below)</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>Symbol (new in ECMAScript 2015)</td>
   <td><code>"symbol"</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>Any other object</td>
   <td><code>"object"</code></td>
  </tr>
 </tbody>
</table>

<p>&nbsp;</p>

<h2 id="Examples">Examples</h2>

<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 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'; 


// Objects
typeof {a:1} === '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 [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';


// Functions
typeof function(){} === 'function';
typeof class C {} === 'function';
typeof Math.sin === 'function';
</pre>

<h3 id="null"><code>null</code></h3>

<pre class="brush:js">
// This stands since the beginning of JavaScript
typeof null === 'object';
</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 in most platforms). Consequently, null had 0 as type tag, hence the bogus <code>typeof</code> return value. (<a href="https://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="https://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.</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>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-typeof-operator', 'The typeof Operator')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-typeof-operator', 'The typeof Operator')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-11.4.3', 'The typeof Operator')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES3', '#sec-11.4.3', 'The typeof Operator')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES1', '#sec-11.4.3', 'The typeof Operator')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.1.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="IE-specific_notes">IE-specific notes</h2>

<p>On IE 6, 7, and 8 a lot of host objects are objects and not functions. For example:</p>

<pre class="brush: js">
typeof alert === 'object'</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/instanceof" title="/en-US/docs/JavaScript/Reference/Operators/instanceof">instanceof</a></code></li>
</ul>
Revert to this revision