mozilla

Revision 17773 of typeof

  • Revision slug: JavaScript/Reference/Operators/typeof
  • Revision title: typeof
  • Revision id: 17773
  • Created:
  • Creator: Hamstersoup
  • Is current revision? No
  • Comment /* Summary */ Updated Math and func to match results on FF2

Revision Content

Summary

The typeof operator is used in either of the following ways:

  1. typeof operand
  2. typeof (operand)

The typeof operator returns a string indicating the type of the unevaluated operand. operand is the string, variable, keyword, or object for which the type is to be returned. The parentheses are optional.

Operator
Implemented in: JavaScript 1.1
ECMA Version: ECMA-262

Suppose you define the following variables:

var myFun = new Function("5+2")
var shape="round"
var size=1
var today=new Date()

The typeof operator returns the following results for these variables:

typeof myFun == 'function'   
typeof shape == 'string'
typeof size == 'number'
typeof today == 'object'
typeof dontExist == 'undefined'

For the keywords true and null, the typeof operator returns the following results:

typeof true == 'boolean'
typeof null == 'object'

For a number or string, the typeof operator returns the following results:

typeof 62 == 'number'
typeof 'Hello world' == 'string'

For property values, the typeof operator returns the type of value the property contains:

typeof document.lastModified == 'string'
typeof window.length == 'number'
typeof Math.LN2 == 'number'

For methods and functions, the typeof operator returns results as follows:

typeof blur == 'function'
typeof eval == 'function'
typeof parseInt == 'function'
typeof shape.split == 'function'

For predefined objects, the typeof operator returns results as follows:

typeof Date == 'function'
typeof Function == 'function'
typeof Math == 'object'
typeof Object == 'function'
typeof String == 'function'
{{ wiki.languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_sp\u00e9ciaux/L\'op\u00e9rateur_typeof", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_specjalne/Operator_typeof" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>The <code>typeof</code> operator is used in either of the following ways:
</p>
<ol><li> <code>typeof <i>operand</i></code>
</li><li> <code>typeof (<i>operand</i>)</code>
</li></ol>
<p>The <code>typeof</code> operator returns a string indicating the type of the unevaluated operand. <code>operand</code> is the string, variable, keyword, or object for which the type is to be returned. The parentheses are optional.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Operator</td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.1</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody></table>
<p>Suppose you define the following variables:
</p>
<pre>var myFun = new Function("5+2")
var shape="round"
var size=1
var today=new Date()
</pre>
<p>The <code>typeof</code> operator returns the following results for these variables:
</p>
<pre>typeof myFun == 'function'   
typeof shape == 'string'
typeof size == 'number'
typeof today == 'object'
typeof dontExist == 'undefined'
</pre>
<p>For the keywords <code>true</code> and <code>null</code>, the <code>typeof</code> operator returns the following results:
</p>
<pre>typeof true == 'boolean'
typeof null == 'object'
</pre>
<p>For a number or string, the <code>typeof</code> operator returns the following results:
</p>
<pre>typeof 62 == 'number'
typeof 'Hello world' == 'string'
</pre>
<p>For property values, the <code>typeof</code> operator returns the type of value the property contains:
</p>
<pre>typeof document.lastModified == 'string'
typeof window.length == 'number'
typeof Math.LN2 == 'number'
</pre>
<p>For methods and functions, the <code>typeof</code> operator returns results as follows:
</p>
<pre>typeof blur == 'function'
typeof eval == 'function'
typeof parseInt == 'function'
typeof shape.split == 'function'
</pre>
<p>For predefined objects, the <code>typeof</code> operator returns results as follows:
</p>
<pre>typeof Date == 'function'
typeof Function == 'function'
typeof Math == 'object'
typeof Object == 'function'
typeof String == 'function'
</pre>
<div class="noinclude">
</div>
{{ wiki.languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_sp\u00e9ciaux/L\'op\u00e9rateur_typeof", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_specjalne/Operator_typeof" } ) }}
Revert to this revision